题解:P12610 [CCC 2025 Junior] Donut Shop

· · 题解

题解

这题看上去很难,实际很简单,我们只需要判断输入的字符是加是减,是加 n 就加 x ,是减 n 就减 x ,最后输出 n 即可。

代码

#include <bits/stdc++.h> //万能头
using namespace std;
#define int long long

int n, x, ans; //定义变量
char c; //定义变量

signed main()
{
    ios::sync_with_stdio(false);
    cin.tie(0), cout.tie(0);
    cin >> ans; //输入ans
    cin >> n; //输入组数
    for (int i = 1; i <= n; i++) //循环读入c, x
    {
        cin >> c >> x; //输入
        if (c == '+') ans += x; //如果加就加
        if (c == '-') ans -= x; //如果减就减
    }
    cout << ans; // 输出答案
    return 0; //完美AC
}