题解:P15638 [ICPC 2022 Tehran R] Final Price

· · 题解

思路

题目描述中只有最后一段有用。

我们仔细读题、观察样例,就可以发现本题要求我们求累加和。我们定义 ans 作为答案,每次输入 x(观察时间段内的每日价格变化),把 ans 加上 x,最后输出的 ans,就是答案了。

Code

#include<bits/stdc++.h>
using namespace std;
int n;
int ans;
int main(){
    ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
    cin>>n;
    for(int i=1;i<=n;i++){
        int x;
        cin>>x;
        ans+=x;
    }
    cout<<ans;
    return 0;
}