提交时间:2024-08-20 21:35:37

运行 ID: 173572

#include <iostream> #include <vector> #include <string> #include <algorithm> using namespace std; string mul(string a, string b) { // 反转从低位开始算起 reverse(a.begin(), a.end()); reverse(b.begin(), b.end()); vector<int> r(a.size() + b.size(), 0); for (int i = 0; i < a.size(); i++) { for (int j = 0; j < b.size(); j++) { r[i + j] += (a[i] - '0') * (b[j] - '0'); r[i + j + 1] += r[i + j] / 10; r[i + j] %= 10; } } // 将结果转成字符串 string s = ""; for (int i = r.size() - 1; i >= 0; i--) { s += to_string(r[i]); } while (s.size() > 1 && s[0] == '0') { s.erase(0, 1); } return s; } int main() { int n, ni, sum = 0; cin >> n; for (int i = 1; i <= n; i++) { cin >> ni; sum += ni; } int t = (n - 1) * (n - 2) / 2; cout << mul(to_string(sum), to_string(t)) << endl; return 0; }