Description
Tom and Jerry are playing a game with tubes and pearls. The rule of the game is:
1) Tom and Jerry come up together with a number K.
2) Tom provides N tubes. Within each tube, there are several pearls. The number of pearls in each tube is at least 1 and at most N.
3) Jerry puts some more pearls into each tube. The number of pearls put into each tube has to be either 0 or a positive multiple of K. After that Jerry organizes these tubes in the order that the first tube has exact one pearl, the 2nd tube has exact 2 pearls, …, the Nth tube has exact N pearls.
4) If Jerry succeeds, he wins the game, otherwise Tom wins.
Write a program to determine who wins the game according to a given N, K and initial number of pearls in each tube. If Tom wins the game, output “Tom”, otherwise, output “Jerry”.
1) Tom and Jerry come up together with a number K.
2) Tom provides N tubes. Within each tube, there are several pearls. The number of pearls in each tube is at least 1 and at most N.
3) Jerry puts some more pearls into each tube. The number of pearls put into each tube has to be either 0 or a positive multiple of K. After that Jerry organizes these tubes in the order that the first tube has exact one pearl, the 2nd tube has exact 2 pearls, …, the Nth tube has exact N pearls.
4) If Jerry succeeds, he wins the game, otherwise Tom wins.
Write a program to determine who wins the game according to a given N, K and initial number of pearls in each tube. If Tom wins the game, output “Tom”, otherwise, output “Jerry”.
Input
The first line contains an integer M (M<=500), then M games follow. For each game, the first line contains 2 integers, N and K (1 <= N <= 100, 1 <= K <= N), and the second line contains N integers presenting the number of pearls in each tube.
Output
For each game, output a line containing either “Tom” or “Jerry”.
Sample Input
2 5 1 1 2 3 4 5 6 2 1 2 3 4 5 5
Sample Output
Jerry Tom
题意:
输入t 组样例
在输入n和k
n代表有n个管子 下一行代表每个管子初始有几个球 可往每个管子里放入k的倍数个球或者不放
如果对应第i个管子里有i个球 那么jerry胜利 否则tom胜利
扫描二维码关注公众号,回复:
1076597 查看本文章
思路:比如第二组样例 输入一个数就把对应的check赋1 表示已经满足第i个管子里有i个球
最后一个5在对应的a[5]++表示有一个多的5
然后找check里为0的数 用他不断减k看是否有a里面有的数
代码:
#include <iostream> #include<stdio.h> #include<algorithm> #include<string.h> using namespace std; int a[105]; int check[105]; int main() { int m,n,k,i,flag,b; cin>>m; while(m--) { cin>>n>>k; flag=0; memset(a,0,sizeof(a)); memset(check,0,sizeof(check)); for(int i=1;i<=n;i++) { scanf("%d",&b); if(check[b]==0) check[b]=1; //表示已满足的 else a[b]++; //表示多余的管子 } for(int i=1;i<=n;i++) //1-n个管子里不满足第i个管子有i个球的 { if(check[i]==0) { int ok=0; //ok是一个判断标志 for(int j=1;i-j*k>=1;j++) //每次用i减去k的倍数 { if(a[i-j*k]>0) //如果a里有 ok标志为1 { ok=1; a[i-j*k]--; //a里减去对应的个数 check[i]=1; //i标记为满足 break; } } if(ok==0) //表示不成功 break掉 { flag=1; break; } } } if(flag==0) printf("Jerry\n"); else printf("Tom\n"); } return 0; }