九度 1095:2的幂次方

本文探讨了一种使用递归函数将正整数表示为其唯一包含0和2的指数形式的方法。通过深入分析案例,理解退出条件、括号处理及加号的生成,实现了一个将任意正整数转换为所需形式的程序。

题目描述:

    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.

 

思路

1. 蛮有意思的一道题, 拿来联系递归函数很好使

2. 题目的核心是递归函数, 而递归函数的核心又是退出条件. 从 137 这个案例可以看出, 当输入是 2 时, 返回 2 , 输入为 1 时返回 2(0). 起初, 我以为 2 的返回应该是 2(2(0)). 这就可以看出, 当 n = 2 时就已经需要退出了, n = 1 时, 自然更要退出了

 

 

3. 括号和加号的处理, 括号在父 DFS 加上(n==1 时例外), 加号由子DFS函数给出, 当 n = 0 时, 没有后续了, 就不用加号

4. 退出条件的位置. 一般来讲, 退出条件要放在 DFS 的上部分, 但这道题放在下面比较合适, 比如当 n = 3 时, 要写成 2 + 2(0)

 

代码

#include <iostream>
#include <stdio.h>
#include <string>
#include <cstring>
using namespace std;

int pow2[20];

void init() {
    pow2[0] = 1;
    for(int i = 1; i < 18; i ++) {
        pow2[i] = 2 * pow2[i-1];
    }

}

void dfs(int n, string &party) {
    for(int i = 16; i > 1; i --) {
        if(n >= pow2[i]) {
            party.push_back('2');
            party.push_back('(');
            dfs(i, party);
            party.push_back(')');
            n = n - pow2[i];
            if(n != 0)
                party.push_back('+');
        }
    }

    if(n >= 2) {
        party.push_back('2');
        n = n -2;
        if(n != 0)
            party.push_back('+');
    }
    if(n != 0) {
        party.append("2(0)");
        n = 0;
    }

}

int n;

int main() {
    freopen("testcase.txt", "r", stdin);
    init();
    while(scanf("%d", &n) != EOF) {
        string str;
        dfs(n, str);
        cout << str << endl;
    }
    return 0;
}

 

转载于:https://www.cnblogs.com/xinsheng/p/3592492.html

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符  | 博主筛选后可见
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值