01 题目来源
http://noi.openjudge.cn/ch0105/07/
02 算法思路
⑴ 金牌总数、银牌总数、铜牌总数,各自用一个变量标识;
⑵ 每次循环时,输入当天获得的金牌数、银牌数、铜牌数,并将它们对应累加到金牌总数、银牌总数、铜牌总数上去。
03 过程步骤
⑴ 定义变量:int类型的n、x、y、z、totalX、totalY、totalZ,分别代表比赛天数、当天获得的金牌数、当天获得的银牌数、当天获得的铜牌数、金牌总数、银牌总数、铜牌总数;
⑵ 令totalX = 0,totally = 0,totalZ = 0,输入n的数值;
⑶ 利用for (int i = 1; i <= n; i++)循环处理如下:
* 输入x、y、z的数值;
* 令totalX = totalX + x,totalY = totalY + y,totalZ = totalZ + z;
⑷ 输出totalX、totally、totalZ、totalX + totalY + totalZ的数值,彼此用一个空格隔开。
04 程序代码
#include <iostream>
using namespace std;
int main()
{
int n;
int x;
int y;
int z;
int totalX = 0;
int totalY = 0;
int totalZ = 0;
cin >> n;
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;
}