P1010 幂次方 dfs

#include <bits/stdc++.h>
using namespace std;
void f(int x,int pos){
	for(int i=pos;i>=0;i--){
		if(pow(2,i)<=x){
			if(i==1) printf("2");
			else if(i==0) printf("2(0)");
			else{
				printf("2(");
				f(i,i);
				printf(")");
			}
			x-=pow(2,i);
			if(x) printf("+");
		}
	}
}
int main(){
	int n;
	scanf("%d",&n);
	f(n,14);//数据小于等于2的14次方
	return 0;
}
发布了84 篇原创文章 · 获赞 3 · 访问量 4788

猜你喜欢

转载自blog.csdn.net/skykone1/article/details/104314979
今日推荐