一直循环

P1010 [NOIP1998 普及组] 幂次方

送你一个正解: ```cpp #include<bits/stdc++.h> using namespace std; string run(int x,int i=0,string s=string("")){ if(x==0)return string("0"); do if(x&1)s=(i==1?"2":"2("+run(i)+")")+(s==""?"":"+")+s; while(++i,x>>=1); return s; } int main(){ int x;cin>>x; cout<<run(x)<<endl; }
by Master2021 @ 2021-10-17 15:16:51


朋友啊,你肯定是重叠了,让测试案例一直到不了你的else后 正解很简单: ``` cpp #include<iostream> using namespace std; int f[20] = {1, 2, 4, 8, 16, 32, 64, 128, 256, 512, 1024, 2048, 4096, 8192, 16384, 32768}; void t(int x){ int i = 0; bool z = 0; for(i = 14; i >= 0; i--){ if(f[i] <= x){ if(i == 1){ if(z){ cout<<"+"; } z = 1; cout << 2; x = x - 2; continue; } if(z){ cout << "+"; } z = 1; cout << "2("; if(i > 2){ t(i); } else if(i == 2){ cout << 2; } else{ cout << "0"; } x = x - f[i]; cout << ")"; } } } int main() { int n = 0; cin >> n; t(n); return 0; } ```
by miku_Silent @ 2021-10-17 16:52:01


|