4 solutions
-
7
像A+B这样的问题,必须采用高精才行
#include <iostream> using namespace std; int w[2][550]; string w1, w2; int main() { cin >> w1; cin >> w2; int x1 = w1.length(), x2 = w2.length(); int t = max(x1, x2), len = 0; for (int i = x1 - 1; i >= 0; i--) { w[0][i] = w1[len++] - '0'; } len = 0; for (int j = x2 - 1; j >= 0; j--) { w[1][j] = w2[len++] - '0'; } for (int i = 0; i < t; i++) { w[0][i] = w[0][i] + w[1][i]; w[0][i + 1] += w[0][i] / 10; w[0][i] %= 10; } while (w[0][t] > 0) t++; while (t != 0 && w[0][t] == 0) t--; for (int i = t; i >= 0; i--) { cout << w[0][i]; } return 0; }
补一发树状数组
#include<iostream> #include<cstring> using namespace std; int lowbit(int a) { return a&(-a); } int main() { int n=2,m=1; int ans[m+1]; int a[n+1],c[n+1],s[n+1]; int o=0; memset(c,0,sizeof(c)); s[0]=0; for(int i=1;i<=n;i++) { cin>>a[i]; s[i]=s[i-1]+a[i]; c[i]=s[i]-s[i-lowbit(i)];//树状数组创建前缀和优化 } for(int i=1;i<=m;i++) { int q=2; { int x=1,y=2;//求a[1]+a[2]的和 int s1=0,s2=0,p=x-1; while(p>0) { s1+=c[p]; p-=lowbit(p);//树状数组求和操作,用两个前缀和相减得到区间和 } p=y; while(p>0) { s2+=c[p]; p-=lowbit(p); } o++; ans[o]=s2-s1;//存储答案 } } for(int i=1;i<=o;i++) cout<<ans[i]<<endl;//输出 return 0; }
- 1
Information
- ID
- 1
- Time
- 1000ms
- Memory
- 256MiB
- Difficulty
- 6
- Tags
- # Submissions
- 1587
- Accepted
- 484
- Uploaded By