欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

【题解】P1010 [NOIP1998 普及组] 幂次方

程序员文章站 2022-05-08 22:52:38
...

直接打表,不用动脑(打 表 次 方)!

思路

反正数据最大2e4也就是说超不过2^15。那么我们直接打表,从0~14都手写出来,然后把输入的数分解了之后就可以直接输出啦!

打表

	out[0] = "2(0)";
	out[1] = "2";
	out[2] = "2(2)";
	out[3] = "2(2+2(0))";
	out[4] = "2(2(2))";
	out[5] = "2(2(2)+2(0))";
	out[6] = "2(2(2)+2)";
	out[7] = "2(2(2)+2+2(0))";
	out[8] = "2(2(2+2(0)))";
	out[9] = "2(2(2+2(0))+2(0))" ;
	out[10] = "2(2(2+2(0))+2)";
	out[11] = "2(2(2+2(0))+2+2(0))" ;
	out[12] = "2(2(2+2(0))+2(2))"  ;
	out[13] = "2(2(2+2(0))+2(2)+2(0))";
	out[14] = "2(2(2+2(0))+2(2)+2)";

全部代码:

#include <cmath>
#include <iostream>
using namespace std;
const int N = 15;
int n;
string out[N];

int main()
{
	out[0] = "2(0)";
	out[1] = "2";
	out[2] = "2(2)";
	out[3] = "2(2+2(0))";
	out[4] = "2(2(2))";
	out[5] = "2(2(2)+2(0))";
	out[6] = "2(2(2)+2)";
	out[7] = "2(2(2)+2+2(0))";
	out[8] = "2(2(2+2(0)))";
	out[9] = "2(2(2+2(0))+2(0))" ;
	out[10] = "2(2(2+2(0))+2)";
	out[11] = "2(2(2+2(0))+2+2(0))" ;
	out[12] = "2(2(2+2(0))+2(2))"  ;
	out[13] = "2(2(2+2(0))+2(2)+2(0))";
	out[14] = "2(2(2+2(0))+2(2)+2)";
	cin >> n; 
	int flag = 1;
	//按数据量来看,2^15就已经大于最大值2e4,所以我们最大的可能也就2的十四次方
	//我们的 i 就从 N = 15 开始分解 
	for(int i = N; i >= 0; i--)
	{
		//拆分数变成2的多少次方的形式
		int t = pow(2, i);
		if(n - t >= 0)
		{
			n -= t;
			//第一个不需要输出加号 
			if(flag == 1)
			{
				cout << out[i];
				flag = 0;
			 } 
			//根据结果进行按表记录 
			else
			{
				cout << ('+' + out[i]);	
			 }
		}	
	}
	return 0;
}