#luogu整理 P1010 幂次方

简单讲下思路

首先一个标准的DFS ,然后打完发现还是GG ,为什么呢?

我们的代码容易将 输出成2(2(0)),然而答案是2

所以加一个特判,如果这个数不是2的一次方,再进行递归就好了

ac代码( 的数组需要预处理):

#include "bits/stdc++.h"
#define int long long
using namespace std;
int a[200] = {1};
void find(int n){
	if(n == 1){cout << "2(0)"; return;}
	if(n == 2){cout << "2";return;}
	int i;
	for(i = 0;i < 200; i++)
		if(a[i] <= n && a[i+1] > n) break;
	cout << "2";
	if(i != 1){
	cout << "(";
	find(i);
	cout << ")";
	}
	if(n > a[i]){
		cout << "+";
		find(n-a[i]);
	}
}
signed main(){
	for(int i = 1;i < 200; i++) a[i] = a[i-1] * 2;
	int n;
	cin >> n;
	find(n);
	return 0;
}//lcez_cyc
原文地址:https://www.cnblogs.com/Cao-Yucong/p/12164578.html