題意:計算兩個數得和c++
#include <bits/stdc++.h> using namespace std; int main() { char a[105],b[105]; int ans[105] = {1}; memset(ans,0,sizeof(ans)); scanf("%s",a); scanf("%s",b); for (int i = 0; i < strlen(a) ; i++) { ans[strlen(a)-i] += a[i] - '0'; } for (int i = 0; i < strlen(b) ; i++) { ans[strlen(b)-i] += b[i] - '0'; } ans[0] = max(strlen(a),strlen(b)); for (int i=1; i <= ans[0] ; i++) { if ( ans[i] < 10)continue; ans[i+1] += ans[i] / 10; ans[i] %= 10; if (i+1 > ans[0]) ans[0] = i+1; } for (int i=ans[0]; i >= 1 ; i--) { printf("%d",ans[i]); } printf("\n"); return 0; }
題意:計算2 ^1000 各位數字累加和
數組
#include <bits/stdc++.h> using namespace std; const int N = 1e5 + 5; const int VAL = 1000; int32_t main() { char a[N],b[N]; int ans[N] ; ans[0] = 1; ans[1] = 1; for (int32_t i = 0; i < 100; i++) { for (int32_t j=1; j <= ans[0] ; j++) { ans[j] *= 1024; } for (int32_t j=1; j <= ans[0]; j++) { if (ans[j] < VAL) continue; ans[j+1] += ans[j] / VAL; ans[j] %= VAL; if (j == ans[0]) ++ans[0]; } } int sum = 0; for (int i=1; i <= ans[0]; i++) { while (ans[i]) { sum += ans[i]%10; ans[i] /= 10; } } cout<<sum<<endl; return 0; }
注意:VAL 的設置:是讓每位ans[i] 保存三位數,必定程度能夠減小數組的長度spa