80分求助(悬赏关注)

P1010 [NOIP1998 普及组] 幂次方

都已经打表了,分一次之后,直接用+号连起来就行。 如:137=str[7]+str[3]+str[0] 不过你的表里下标没有和2的次幂对齐,如果表的下标代表2的n次幂string str[15]={"2(0)","2","2(2)",....}
by daydayup12 @ 2023-11-15 21:13:51


## 在处理Str时要做特判,注意题目说2的1次方直接输出为2不输出为2(2(0)),所以遇到2的1次方时Str+="2+"; ```cpp #include <bits/stdc++.h> using namespace std; int n; string a; int mpow(int p) { if (p == 0) return (1); if (p == 1) return (2); if (p % 2) return (mpow(p - 1) * 2); int temp = mpow(p / 2); return (temp * temp); } vector<int> liebiao; void init(int yu) { if (yu == 0) return; if (yu == 1) { liebiao.push_back(0); return; } int Y = log2(yu); liebiao.push_back(Y); yu -= mpow(Y); init(yu); } string str[20] = {"0", "2(0)", "2", "2+2(0)", "2(2)", "2(2)+2(0)", "2(2)+2", "2(2)+2+2(0)", "2(2+2(0))", "2(2+2(0))+2(0)", "2(2+2(0))+2", "2(2+2(0))+2+2(0)", "2(2+2(0))+2(2)", "2(2+2(0))+2(2)+2(0)", "2(2+2(0))+2(2)+2", "2(2+2(0))+2(2)+2+2(0)"}, Str; int main() { cin >> n; init(n); // for(int i=0;i<liebiao.size();i++) cout<<liebiao[i]<<" "; for (int i = 0; i < liebiao.size() - 1; i++) { if(liebiao[i]==1)//注意这个if是新加的 { Str += "2+"; } else Str += "2(" + str[liebiao[i]] + ")+"; } Str += "2(" + str[liebiao[liebiao.size() - 1]] + ")"; cout << Str; return (0); } ```
by LiHS_ @ 2023-11-17 22:44:42


@[caojiaming](/user/775551) 不是分治吗?打表看得更加懵逼。 分治的 ```merge()``` 函数可以参考一下。 ```cpp while (x){ int power=log2(x); if (power==0) printf("2(0)"); else if (power==1) printf("2"); else{ printf("2("); merge(power); printf(")"); } x-=pow(2,power); if (x) printf("+"); } ```
by I_like_play_eggy @ 2024-01-29 19:19:52


|