这凭什么能过呀
查看原帖
这凭什么能过呀
589670
OwenTZC楼主2021/11/2 07:28

随手写的递归 第一次没有讨论x=0x=0的返回值,报错

第二次没有讨论x>pow(2,t) x>pow(2,t),报错

第三次没有讨论x=3x=3的返回值,答案怪怪的

感觉讨论到x=2x=2就差不多了,但会出现2=2(2(0))2=2(2(0))恼火

#include <bits/stdc++.h>

using namespace std;

string represent(int x)
{
    //printf("x = %d\n", x);
    if(x==0) return "";
    if(x==1) return "2(0)";
    if(x==2) return "2";
    if(x==3) return "2+2(0)";
    string s = "";
    int t = log2(x);
    s += "2(" + represent(t) +")";
    if(x>pow(2,t))
    s += "+" + represent(x-pow(2,t));
    return s;
}

int main()
{
    int n;
    cin >> n;
    cout << represent(n);
    return 0;
}

2021/11/2 07:28
加载中...