【題解】ZeroJudge f444: 10268 – 498-bis

【題目敘述】https://zerojudge.tw/ShowProblem?problemid=f444 (考生答對率: 17.9%)

#include <iostream>
#include <string>
#include <sstream>
#include <vector>
#include <algorithm>
using namespace std;

string s;
int x;
vector <int> v;

int main() {
    while (cin >> x){
        getline(cin, s);
        getline(cin, s);
        //cout << "s: " << s << "\n";
        stringstream ss(s);
        v.clear();
        while (ss >> s){
            v.push_back(stoi(s));
        }
        v.pop_back();
        reverse(v.begin(), v.end());
        long long mul = 1;
        int ans = 0;
        for (int i = 0; i < v.size(); i++){
            //cout << "i: " << i << " " << v[i] << " " << ans << " " << mul << "\n";
            ans += v[i]*(i+1)*mul;
            mul *= x;
        }
        cout << ans << "\n";
    }
}

Python code (credit: Amy Chou)

while True:
    try:
        x = int(input())
        A = list(map(int, input().strip().split()))
        ans = 0
        n = len(A) - 1
        for i in range(n):
            ans += A[i] * (n-i) * pow(x, n-1-i)
        print(int(ans))
    except:
        break
分享本文 Share with friends