P1010 [NOIP1998 普及组] 幂次方

原题链接
考察:分治
思路:
  我是sb,普及题不会().发现只要不是2两端就需要(),由此区别开.

Code

#include <iostream> 
#include <algorithm>
using namespace std;
const int N = 20;
int logs[N],n;
int findm(int n)
{
	int l = 0,r = N;
	while(l<r)
	{
		int mid = l+r+1>>1;
		if(logs[mid]<=n) l =mid;
		else r = mid-1;
	}
	return r;
}
string solve(int n)
{//分解为2和2(0) 
	if(!n) return "0";
	int idx = findm(n);
	string s;
	if(idx==1) s += "2";//如果不是2,就+() 
	else s = s+"2("+solve(idx)+")";
	if(n-logs[idx]>0) s = s+"+";//还有就+ 
	else if(n==logs[idx]) return s;//相等回去 
	s = s+solve(n-logs[idx]);
	return s;
}
int main()
{
	for(int i=0;i<N;i++) logs[i] = 1<<i; 
	scanf("%d",&n);
	printf("%s\n",solve(n).c_str());
	return 0;
}
posted @ 2021-06-22 10:09  acmloser  阅读(170)  评论(0编辑  收藏  举报