一、题目链接
http://noi.openjudge.cn/ch0105/07/
二、解题思路
◎ 以步长1为基准,循环输入每天获得的金牌数、银牌数、铜牌数,并按照金牌、银牌、铜牌三类分别累加求和;
◎ 循环结束后,金牌、银牌、铜牌三类的总和就是奖牌总数。
三、实施步骤
◎ 首先,定义并输入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、totalX+totalY+totalZ。
四、Java程序
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int n = input.nextInt();
int x;
int y;
int z;
int totalX = 0;
int totalY = 0;
int totalZ = 0;
for (int i = 1; i <= n; i++) {
x = input.nextInt();
y = input.nextInt();
z = input.nextInt();
totalX = totalX + x;
totalY = totalY + y;
totalZ = totalZ + z;
}
System.out.print(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;
}