P1010 幂次方

这么难得题,居然普及-?做了好久

#include <bits/stdc++.h>
using namespace std;
int fact[21];
void solve(int n) {
	if(n == 0) {cout << 0; return;}
	if(n == 2) {cout << 2; return;}
	while(n) {
		int x = floor(log(n)/log(2));
		if(x!=1){cout << 2 << '(';
		solve(x);} else {cout << 2;
		}
		n-=fact[x];
		if(x!=1)cout << ')' ;
		if(n) cout << '+';
	}
}
int main() {
	int ans = 1;
	for(int i = 0; i<=20; i++) {
		fact[i] = ans;
		ans *= 2;
	}
	int n;
	cin >> n;
	solve(n);
	return 0;
}

posted on 2016-11-03 18:46  蒟蒻konjac  阅读(226)  评论(0编辑  收藏  举报