递归 | 洛谷 | P1010

版权声明:本文纯属作者口胡,欢迎转载 https://blog.csdn.net/TQCAI666/article/details/86491216

https://www.luogu.org/problemnew/show/P1010

#include <bits/stdc++.h>
#define FF(a,b) for(int a=0;a<b;a++)
#define F(a,b) for(int a=1;a<=b;a++)
#define LEN 20
#define INF 1000000
#define bug(x) cout<<#x<<"="<<x<<endl;

using namespace std;

typedef long long ll;

void f(int x,bool init=false){
    if(x==2)
        printf("2(2)");
    else if(x==1)
        printf("2");
    else if(x==0)
        printf("2(0)"); //三个递归出口
    else{
        if(!init)
            printf("2(");
        stack<int> s;//暂存
        int i=0;
        while(x){
            if(x&1)
                s.push(i);
            i++;
            x>>=1;
        }
        while(!s.empty()){
            f(s.top());
            s.pop();
            if(s.size()) putchar('+');
        }
        if(!init)
            printf(")");
    }
}

int main()
{
    // freopen("./in","r",stdin);
    int num;
    scanf("%d",&num);
    f(num,true);
    return 0;
}

猜你喜欢

转载自blog.csdn.net/TQCAI666/article/details/86491216
今日推荐