一、题目链接
http://noi.openjudge.cn/ch0105/07/
二、解题思路
◎ 循环输入每天获得的金牌数、银牌数、铜牌数,并分别累加到金牌总数、银牌总数、铜牌总数上;
◎ 循环结束后,金牌总数、银牌总数、铜牌总数之和就是奖牌总数。
三、实施步骤
◎ 首先,定义并输入int类型的整数n,代表比赛天数;
◎ 其次,定义六个int类型的整数x、y、z、totalX、totalY、totalZ,分别代表每天获得的金牌数、银牌数、铜牌数、金牌总数、银牌总数、
铜牌总数,初始时totalX、totalY、totalZ均为0;
◎ 然后,通过标记i代表比赛天数,i从1开始,到n为止,更新步长为1,循环处理如下:
→ 输入当天获得的金牌数x、银牌数y、铜牌数z;
→ 令totalX=totalX+x,totalY=totalY+y,totalZ=totalZ+z;
◎ 最后,输出totalX、totalY、totalZ、totalX+totalY+totalZ。
四、C++程序
#include <iostream>
using namespace std;
int main()
{
int n;
cin >> n;
int x;
int y;
int z;
int totalX = 0;
int totalY = 0;
int totalZ = 0;
for (int i = 1; i <= n; i++)
{
cin >> x;
cin >> y;
cin >> z;
totalX = totalX + x;
totalY = totalY + y;
totalZ = totalZ + z;
}
cout << totalX << " " << totalY << " " << totalZ << " " << totalX + totalY + totalZ;
return 0;
}