JAVA—小鑫の日常系列故事(五)——卡片游戏
Description
小鑫和健健喜欢在课间的时候玩游戏,今天他手里有n张卡片,每张卡片有一个数字。于是他就想和健健比一比更幸运。玩法是这样的:这n张卡片上的数字是没有规律的。他俩轮流取走最上面的一张卡片,最后谁取走卡片上的数字之和最大,谁就更幸运。(因为这个游戏是小鑫想出来的,总是小鑫拿第一张)你能帮他们判断谁更幸运么?Input第一行为一个数字 n。100000>n>0第二行为n个数,是n张卡片上的数字,第一个数为最上面的卡片上的数,依次向下。N个数是在整型范围内。Output如果小鑫更幸运输出“Greater than”,如果健健更幸运输出“Less than”,否则“Equal”。(输出均不含引号)
SampleInput
6
1 1 2 2 3 3
Output
Equal
import java.util.Scanner;
public class Main { public static void main(String[] args) { // TODO Auto-generated method stub Scanner reader = new Scanner(System.in);
int sum1 = 0, sum2 = 0, i, n, c;
n = reader.nextInt();
for (i = 1; i <= n; i++) {
c = reader.nextInt();
if (i % 2 != 0) {
sum1 = sum1 + c;
} else {
sum2 = sum2 + c;
}
}
if (sum1 > sum2) {
System.out.print("Greater than");
} else if (sum1 < sum2) {
System.out.print("Less than");
} else {
System.out.print("Equal");
}
}
}