九度OJ 1095:2的幂次方 (递归)
2015-10-23 08:55
337 查看
时间限制:1 秒
内存限制:32 兆
特殊判题:否
提交:913
解决:626
题目描述:
Every positive number can be presented by the exponential form.For example, 137 = 2^7 + 2^3 + 2^0。
Let's present a^b by the form a(b).Then 137 is presented by 2(7)+2(3)+2(0). Since 7 = 2^2 + 2 + 2^0 and 3 = 2 + 2^0 , 137 is finally presented by 2(2(2)+2 +2(0))+2(2+2(0))+2(0).
Given a positive number n,your task is to present n with the exponential form which only contains the digits 0 and 2.
输入:
For each case, the input file contains a positive integer n (n<=20000).
输出:
For each case, you should output the exponential form of n an a single line.Note that,there should not be any additional white spaces in the line.
样例输入:
样例输出:
来源:2006年上海交通大学计算机研究生机试真题
思路:
需用递归来做,注意边界条件,2不应该被打印成2(0).
代码:
内存限制:32 兆
特殊判题:否
提交:913
解决:626
题目描述:
Every positive number can be presented by the exponential form.For example, 137 = 2^7 + 2^3 + 2^0。
Let's present a^b by the form a(b).Then 137 is presented by 2(7)+2(3)+2(0). Since 7 = 2^2 + 2 + 2^0 and 3 = 2 + 2^0 , 137 is finally presented by 2(2(2)+2 +2(0))+2(2+2(0))+2(0).
Given a positive number n,your task is to present n with the exponential form which only contains the digits 0 and 2.
输入:
For each case, the input file contains a positive integer n (n<=20000).
输出:
For each case, you should output the exponential form of n an a single line.Note that,there should not be any additional white spaces in the line.
样例输入:
1315
样例输出:
2(2(2+2(0))+2)+2(2(2+2(0)))+2(2(2)+2(0))+2+2(0)
来源:2006年上海交通大学计算机研究生机试真题
思路:
需用递归来做,注意边界条件,2不应该被打印成2(0).
代码:
#include <stdio.h> void present(int n) { if (n == 0 || n == 2) { printf("%d", n); return; } int i; int a[20], c; for (i=0; n>0; i++) { a[i] = n%2; n /= 2; } c = i; for (i=c-1; i>=0; i--) { if (a[i] == 0) continue; if (i != c-1) printf("+"); if (i == 1) { printf("2"); continue; } printf("2("); present(i); printf(")"); } } int main(void) { int n; while (scanf("%d", &n) != EOF) { present(n); printf("\n"); } return 0; } /************************************************************** Problem: 1095 User: liangrx06 Language: C Result: Accepted Time:0 ms Memory:912 kb ****************************************************************/
相关文章推荐
- 九度OJ 1095:2的幂次方 (递归)
- 2015-10-23 【项目5 - 计数的模式匹配】
- [MSSQL]磁盘空间&SQL日志&作业执行情况
- JavaScript权威指南_169_第17章_事件处理_17.3-调用事件处理程序-事件传播、事件取消
- 第7周项目3 - 负数把正数赶出队列
- hdu4258 斜率优化dp
- HDOJ 2243 考研路茫茫——单词情结(AC自动机+矩阵快速幂)
- 第七周项目6--停车场
- 九度OJ 1094:String Matching(字符串匹配) (计数)
- 九度OJ 1094:String Matching(字符串匹配) (计数)
- 第8周项目1——建立顺序串的算法库
- 排队看病模拟
- Android ExpandableListView开发简介
- Objective-C 类,对象,属性,方法
- java正则表达式学习
- swift中关于表情键盘,抖动问题处理
- 2015-10-23 【项目4-字符串加密】
- 第9周 项目3—稀疏矩阵的三元组表示的实现及应用
- 用switch语句实现多分支结构
- 九度OJ 1093:WERTYU (翻译)