Game with Pearls

Game with Pearls

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 3685    Accepted Submission(s): 1293


Problem 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”.
 
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
 
题意:Jerry和Tom两个人玩游戏,Tom提供N个管子,并且在管子里放珍珠(数量至少一个),并且给出了一个k,现在
Jerry要在这些管子里再放一些珍珠,使得第一个管子里有一个珍珠,第二个管子里有两个珍珠,以此类推,不管之前的顺序怎样,
最后放完后排序之后要符合上面说的规则.
思路:
  每次排序一次,找到比i小的就加上k,然后再排序.
 1 #include <iostream>
 2 #include <cstring>
 3 #include <algorithm>
 4 #define mem(a) memset(a,0,sizeof(a))
 5 using namespace std;
 6 
 7 int a[105];
 8 int main(){
 9     int n;
10     cin>>n;
11     while(n--){
12         memset(a,0,sizeof(a));
13         int m,k;
14         cin>>m>>k;
15         bool prime = true;
16         for(int i=1;i<=m;i++){
17             cin>>a[i];
18         }
19         while(prime){
20             sort(a+1,a+1+m);
21             bool flag = true;
22             for(int i=1;i<=m;i++){
23                 if(a[i]<i){
24                     a[i]+=k;
25                     flag = false;
26                     break;
27                 }
28                 else if(a[i]>i){
29                     prime = false;
30                     break;
31                 }
32             }
33             if(flag) break;
34         }
35         if(prime){
36             cout<<"Jerry"<<endl;
37         }else{
38             cout<<"Tom"<<endl;
39         }
40     }
41     return 0;
42 }
 
 
原文地址:https://www.cnblogs.com/zllwxm123/p/9340381.html