博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
九度OJ 1095:2的幂次方 (递归)
阅读量:4206 次
发布时间:2019-05-26

本文共 1610 字,大约阅读时间需要 5 分钟。

时间限制: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.

样例输入:
1315
样例输出:
2(2(2+2(0))+2)+2(2(2+2(0)))+2(2(2)+2(0))+2+2(0)
来源:

思路:

需用递归来做,注意边界条件,2不应该被打印成2(0).

代码:

#include 
 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****************************************************************/

转载地址:http://dfeli.baihongyu.com/

你可能感兴趣的文章
面试刷题29:mysql事务隔离实现原理?
查看>>
面试刷题30:SpringBean的生命周期?
查看>>
面试刷题31:分布式ID设计方案
查看>>
面试刷题32:你对tomcat做了哪些性能调优?
查看>>
面试刷题33:如何应对高并发?
查看>>
面试刷题34:说一下分布式架构中的缓存使用场景?
查看>>
kafka的基本体系结构
查看>>
如何保证kafka消息不丢失
查看>>
Struts 2 文件上传
查看>>
创建flex的web工程的方法(总结)
查看>>
SQL Transcation的一些总结
查看>>
JDBC--Statement,PreparedStatement,CallableStatement的区别
查看>>
IoC/依赖注入、以及在Spring中的实现 (转载)
查看>>
在Struts2中jsp前台传值到action后台的方法
查看>>
jsp与数据库的链接编码问题
查看>>
HttpSession理解 .
查看>>
SpringAOP
查看>>
javamail发送邮件
查看>>
jQuery技巧
查看>>
DOM和SAX对比
查看>>