一、题目链接
http://noi.openjudge.cn/ch0105/07/
二、解题思路
◎ 循环输入每天获得的金牌数、银牌数、铜牌数,并分别累加到金牌总数、银牌总数、铜牌总数上;
◎ 循环结束后,金牌总数、银牌总数、铜牌总数之和就是奖牌总数。
三、实施步骤
◎ 定义并输入int类型的整数n,代表比赛天数;
◎ 定义六个int类型的整数x、y、z、totalX=0、totalY=0、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。
四、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));
}
}