題目背景
Score: 15.
題目描述
The owner of a donut shop spends the day baking and selling donuts.
Given the events that happen over the course of the day, your job is to determine the number of donuts remaining when the shop closes.
輸入格式
The first line of input contains a non-negative integer,?D, representing the number of donuts available when the shop first opens.
The second line contains a positive integer,?E, representing the number of events that happen over the course of the day. The next?E?pairs of input lines describe these events. The first line in the pair contains either the?+?(plus) symbol, indicating that donuts have been baked, or the???(minus) symbol, indicating that donuts have been sold. The second line in the pair contains a positive integer,?Q, representing the quantity of donuts associated with the event.
For each sale of donuts, the value of?Q?will be less than or equal to the number of donuts available at that time.
輸出格式
Output the non-negative integer,?R, which is the number of donuts remaining when the shop closes.
輸入輸出樣例
in:
10
3
+
24
-
6
-
12
out:
16
說明/提示
Explanation of Output for Sample Input
The shop opened with?10?donuts and there were?3?events during the day. The owner first baked?24?donuts. Then the owner sold?6?donuts, followed by another?12. The number of donuts remaining is?10+24?6?12=16.
這道題是不是有些看不懂?
我來翻譯一下
題目背景
分數:15 分。
題目描述 一家甜甜圈店的店主整日都在烘焙和售賣甜甜圈。
根據一天中發生的各種事件,你的任務是確定店鋪打烊時剩余的甜甜圈數量。
輸入格式 輸入的第一行包含一個非負整數D,表示店鋪剛開門時可供售賣的甜甜圈數量。
第二行包含一個正整數E,表示一天中發生的事件數量。接下來的E組輸入行描述了這些事件。每組的第一行包含 “\(+\)”(加號)符號,表示又烘焙了一些甜甜圈;或者包含 “\(-\)”(減號)符號,表示賣出了一些甜甜圈。每組的第二行包含一個正整數Q,表示與該事件相關的甜甜圈數量。
對于每一次甜甜圈的售賣,Q的值將小于或等于當時可供售賣的甜甜圈數量。
輸出格式 輸出一個非負整數R,即店鋪打烊時剩余的甜甜圈數量。
輸入輸出樣例
in:
10
3
+
24
-
6
-
12
out:
16
說明/提示
店鋪一開始有 10 個甜甜圈,一天內發生了 3 件事。店主首先烘焙了 24 個甜甜圈。然后店主賣出了 6 個甜甜圈,接著又賣出了 12 個。剩余的甜甜圈數量為 10 + 24 - 6 - 12 = 16 個。
Problem
你有?D?個甜甜圈,
共有?E?個甜甜圈的變化事件,
每個事件有兩種可能的情況:
增加或者減少?Q?個甜甜圈。
求剩余的甜甜圈數量。
Solution
對于每個事件,
對甜甜圈的初始數?D?加或減每個對應的?Q?即可。
那么最終?D?的值即為所求結果。
用循環結構簡單模擬就可以,每次輸入判斷是加還是減法,直接對?D?進行運算即可。
只要判斷每次輸入的符號是?+
?還是?-
?即可。
如果是?+
,
那么總甜甜圈數加上輸入的個數。
否則就減去輸入的個數。
Code
#include<bits/stdc++.h>
using namespace std;
int main(){int d,e;cin>>d>>e;while(e--){char c;cin>>c;int q;cin>>q;if(c=='+') d+=q;else d-=q;}cout<<d;return 0;
}
聽說給點贊,關注,收藏的人會發財哦(づ ̄3 ̄)づ╭?~