题目传送:蓝桥杯 - 2的次幂表示
思路:这递归递的我头都晕了,先贴个伪递归吧
AC代码:
#include <cstdio> #include <cstring> #include <algorithm> using namespace std; void fun1(int a) { //输出2的a次方,因为题目最大就20000,2的15次方足以 if(a == 0) printf("2(0)"); else if(a == 1) printf("2"); else if(a == 2) printf("2(2)"); else if(a == 3) printf("2(2+2(0))"); else if(a == 4) printf("2(2(2))"); else if(a == 5) printf("2(2(2)+2(0))"); else if(a == 6) printf("2(2(2)+2)"); else if(a == 7) printf("2(2(2)+2+2(0))"); else if(a == 8) printf("2(2(2+2(0)))"); else if(a == 9) printf("2(2(2+2(0))+2(0))"); else if(a == 10) printf("2(2(2+2(0))+2)"); else if(a == 11) printf("2(2(2+2(0))+2+2(0))"); else if(a == 12) printf("2(2(2+2(0))+2(2))"); else if(a == 13) printf("2(2(2+2(0))+2(2)+2(0))"); else if(a == 14) printf("2(2(2+2(0))+2(2)+2)"); else if(a == 15) printf("2(2(2+2(0))+2(2)+2+2(0))"); } void fun2(int a, int cur) { if(a == 0) return; int t = a % 2; int tmp = a / 2; fun2(tmp, cur + 1); if(t == 1) { if(tmp != 0) printf("+"); fun1(cur); } } int main() { int n; while(scanf("%d", &n) != EOF) { fun2(n, 0); printf("\n"); } return 0; }
时间: 2024-11-06 07:23:53