水题应该说有些基础的都会,主要是输出格式的问题
Undercut is a card game where two players each have five cards numbered one through five. At each round, each player selects a card, then simultaneously reveals it. If the cards are of equal value, there is no score. Otherwise, there are two cases: the two cards are exactly one point apart (this is called an undercut), or the cards are more than one point apart. In the latter case, the person revealing the larger of the cards gets the number of points on the larger card. In the case of an undercut the player with the lower card gets the sum of the two cards. The exception to this is when the cards are 1 and 2, in which case the player with the lower card gets 6 points (instead of only 3 points). After each round, the cards are returned to the hands and they play another round.
For example, if there are 5 rounds and player A plays (in this order) 5, 3, 1, 3, 5 and player B plays 3, 3, 3, 3, 4, then the scoring for each round would be: A gets 5 points, no points, B gets 3 points, no points, B gets 9 points. The totals would be A: 5, B: 12.
In this problem you will be given card plays for both players and must determine the final scores.
Input
There will be multiple input instances. Each instance will be one game. The first line of input for a game will be an integer n <= 20. (A value of n = 0 terminates input.) The next two lines will each contain n integers between 1 and 5 inclusive indicating the cards played on each of n rounds. The first line are player A's card plays and the second line are player B's card plays.
Output
Each input instance should generate one line of output of the form:
A has a points. B has b points.
where the value of a and b are for you to determine. A blank line should separate output lines.
Sample Input
5
5 3 1 3 5
3 3 3 3 4
4
2 3 1 1
1 5 5 5
0
Sample Output
A has 5 points. B has 12 points.
A has 0 points. B has 21 points.
#include<stdio.h>
#include<stdlib.h>
int a[20],b[20];
int main()
{
int n,t;
scanf("%d",&n); //因为这道题的输出格式所以把一个输入写在了这里
while(n)
{
int A,B;
A=B=0; //分数清零
for(int i=0;i<n;i++)
scanf("%d",&a[i]);
for(int i=0;i<n;i++)
scanf("%d",&b[i]);
for(int i=0;i<n;i++)
{
if(abs(a[i]-b[i])>1) //差大于2
{
t=a[i]>b[i]?a[i]:b[i]; //t=Max(a[i],b[i])
if((a[i]-b[i])>0) //比较大小,差大于2是较大的牌一方得分
A+=t;
else
B+=t;
}
else if(abs(a[i]-b[i])==1) //差等于1
{
t=a[i]+b[i];
if((a[i]==1&&b[i]==2)||(a[i]==2&&b[i]==1)) //单独列出1,2
t*=2; //当是1,2时分数是6,也是t*2
if((a[i]-b[i])<0) //差等于1时较小一方得分
A+=t;
else
B+=t;
}
}
printf("A has %d points. B has %d points.\n",A,B);
scanf("%d",&n); //每一次在结尾接收下一次的n,这样可以判断是否需要输出一行空白
if(n)
putchar('\n');
else
break; //如果n=0则break
}
return 0;
}