时间限制: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)
来源:
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的幂次方 (递归)的更多相关文章

  1. 九度OJ 1514 数值的整数次方【算法】

    题目地址:http://ac.jobdu.com/problem.php?pid=1514 题目描述: 给定一个double类型的浮点数base和int类型的整数exponent.求base的expo ...

  2. 九度OJ 1084:整数拆分 (递归)

    时间限制:1 秒 内存限制:32 兆 特殊判题:否 提交:2274 解决:914 题目描述: 一个整数总可以拆分为2的幂的和,例如: 7=1+2+4 7=1+2+2+2 7=1+1+1+4 7=1+1 ...

  3. 九度OJ 1006:ZOJ问题 (递归)

    时间限制:1 秒 内存限制:32 兆 特殊判题:否 提交:18621 解决:3197 题目描述: 对给定的字符串(只包含'z','o','j'三种字符),判断他是否能AC. 是否AC的规则如下: 1. ...

  4. 九度OJ 1122:吃糖果 (递归)

    时间限制:1 秒 内存限制:32 兆 特殊判题:否 提交:1522 解决:1200 题目描述: 名名的妈妈从外地出差回来,带了一盒好吃又精美的巧克力给名名(盒内共有 N 块巧克力,20 > N ...

  5. 【九度OJ】题目1474:矩阵幂 解题报告

    [九度OJ]题目1474:矩阵幂 解题报告 标签(空格分隔): 九度OJ http://ac.jobdu.com/problem.php?pid=1474 题目描述: 给定一个n*n的矩阵,求该矩阵的 ...

  6. 【九度OJ】题目1441:人见人爱 A ^ B 解题报告

    [九度OJ]题目1441:人见人爱 A ^ B 解题报告 标签(空格分隔): 九度OJ 原题地址:http://ac.jobdu.com/problem.php?pid=1441 题目描述: 求A^B ...

  7. 【九度OJ】题目1015:还是A+B 解题报告

    [九度OJ]题目1015:还是A+B 解题报告 标签(空格分隔): 九度OJ http://ac.jobdu.com/problem.php?pid=1015 题目描述: 读入两个小于10000的正整 ...

  8. 【九度OJ】题目1442:A sequence of numbers 解题报告

    [九度OJ]题目1442:A sequence of numbers 解题报告 标签(空格分隔): 九度OJ 原题地址:http://ac.jobdu.com/problem.php?pid=1442 ...

  9. 【九度OJ】题目1087:约数的个数 解题报告

    [九度OJ]题目1087:约数的个数 解题报告 标签(空格分隔): 九度OJ 原题地址:http://ac.jobdu.com/problem.php?pid=1087 题目描述: 输入n个整数,依次 ...

随机推荐

  1. 转载 关于malloc

    1.函数原型及说明: void *malloc(long NumBytes):该函数分配了NumBytes个字节,并返回了指向这块内存的指针.如果分配失败,则返回一个空指针(NULL). 关于分配失败 ...

  2. GNOME 3.x下安装配置小企鹅输入法框架及SunPinYin插件

    fcitx 小企鹅输入法框架已经越来越成熟,并且具备极高的性能,配合 Sun PinYin 智能输入法就和 Windows 下的搜狗百度等输入法几乎无二了.事实上,现在Linux版本的搜狗输入法正是基 ...

  3. 广播broadcast的使用

    很多时候我们有这样的需求,比如说,订单支付成功,需要更新订单列表或订单详情的订单状态,这时候我们就可以用到广播. 首先我们要使用Intent来发送一个广播 定义一个全局的广播名字 public sta ...

  4. Python的格式化输出,基本运算符,编码

    一. 格式化输出现在有以下需求,让用户输入name, age, job,hobby 然后输出如下所示: -----------info of Alex Li----------- Name : Ale ...

  5. Codeforces 946 C.String Transformation

    C. String Transformation   time limit per test 1 second memory limit per test 256 megabytes input st ...

  6. Office 各版本下载链接

    Office 2007 链接: https://pan.baidu.com/s/1pNJDlafw6KQSlljRUAQtWw 提取码: xoml 密钥:DBXYD-TF477-46YM4-W74MH ...

  7. MyBatis_SelectKey使用oracle 序列插入主键

    mapper 如下: 使用<selectkey>实现 也可以使用oracle的row 级触发器trigger实现: <?xml version="1.0" enc ...

  8. Java原子类及内部原理

    一.引入 原子是世界上的最小单位,具有不可分割性.比如 a=0:(a非long和double类型) 这个操作是不可分割的,那么我们说这个操作是原子操作.再比如:a++: 这个操作实际是a = a + ...

  9. dtrace 语法

    Usage: dtrace [-aACeFHlqSvVwZ] [-arch i386|x86_64] [-b bufsz] [-c cmd] [-D name[=def]]      [-I path ...

  10. javascript 函数初探 (五)--- 几种类型的函数

    即时函数: 目前我们已经讨论了匿名函数在回调时的应用.接下来,我们来看看匿名函数的另一种应用实例 --- javascript即时函数: 比如: ( function(){ alert('her'); ...