b:2的幂次方表示()
-
总时间限制:
- 1000ms 内存限制:
- 65536kB
-
描述
-
任何一个正整数都可以用2的幂次方表示。例如:
137=27+23+20
同时约定方次用括号来表示,即ab可表示为a(b)。由此可知,137可表示为:
2(7)+2(3)+2(0)
进一步:7=22+2+20(21用2表示)
3=2+20
所以最后137可表示为:
2(2(2)+2+2(0))+2(2+2(0))+2(0)
又如:
1315=210+28+25+2+1
所以1315最后可表示为:
2(2(2+2(0))+2)+2(2(2+2(0)))+2(2(2)+2(0))+2+2(0)
输入
- 一个正整数n(n≤20000)。 输出
- 一行,符合约定的n的0,2表示(在表示中不能有空格)。 样例输入
-
137
样例输出
-
2(2(2)+2+2(0))+2(2+2(0))+2(0)
来源
- NOIP1998复赛 普及组 第一题
-
#include <algorithm> #include <iostream> #include <cstring> #include <string> #include <vector> #include <cstdio> #include <queue> #include <cmath> #include <map> using namespace std; #define N 5010 #define INF 0x3f3f3f3f #define mod 100003 #define EPS 1e-12 char s[10]; void solution(int n) { if(n==1) { printf("2(0)"); return ; } else if(n==2) { printf("2"); return ; } else { int m=1,e=0;//m^e while(m<=n){ m*=2; e++; } m/=2; e--;//回退 if(e==1) { printf("2"); } else { printf("2("); solution(e); printf(")"); } if(m!=n) {//如果还有剩余继续 printf("+"); solution(n-m); } } return ; } int main() { int n; scanf("%d",&n); solution(n); return 0; }