提交时间:2023-10-10 13:24:18

运行 ID: 106189

#include <bits/stdc++.h> using namespace std; int c[114514]; string add(string a_, string b_) { int maxl = 0; string a="",b="",res=""; memset(c,0,sizeof(c)); for(int i = a_.length()-1; i >= 0; i--) { a += a_[i]; } for(int i = b_.length()-1; i >= 0; i--) { b += b_[i]; } //补零 if(a_.length()>b_.length()) { for(int i = 1; i <= a_.length()-b_.length(); i++)b+='0'; } if(b_.length()>a_.length()) { for(int i = 1; i <= b_.length()-a_.length(); i++)a+='0'; } maxl = max(a.length(),b.length()); //模拟竖式 for(int i = 0; i < maxl; i++) { c[i] += a[i]+b[i]-'0'-'0'; c[i+1] += c[i]/10; c[i]%=10; } //例如99+1 = 100, 那么会产生多一位 if(c[maxl]) { maxl++; } //输出,因为算的是反过来的,所以输出再反过来,就正常了 for(int i = maxl-1; i >= 0; i--) { res += c[i]+'0'; } return res; } signed main() { int n; string kk; string ans = "0"; cin >> n; for(int i = 1; i <= n; i++) { cin >> kk; for(int j = 1; j <= n-1; j++) { ans = add(ans,kk); } } cout << ans << endl; return 0; }