A - Game with Pearls(最大匹配)

版权声明:莉莉莉 https://blog.csdn.net/qq_41700151/article/details/80342685

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


题意:
输入t 组样例
在输入n和k
n代表有n个管子 下一行代表每个管子初始有几个球 可往每个管子里放入k的倍数个球或者不放

如果对应第i个管子里有i个球 那么jerry胜利 否则tom胜利

思路:比如第二组样例 输入一个数就把对应的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;
}

Pearls Pearls Pearls Pearls Pearls

09-22

Problem DescriptionrnIn Pearlania everybody is fond of pearls. One company, called The Royal Pearl, produces a lot of jewelry with pearls in it. The Royal Pearl has its name because it delivers to the royal family of Pearlania. But it also produces bracelets and necklaces for ordinary people. Of course the quality of the pearls for these people is much lower then the quality of pearls for the royal family. In Pearlania pearls are separated into 100 different quality classes. A quality class is identified by the price for one single pearl in that quality class. This price is unique for that quality class and the price is always higher then the price for a pearl in a lower quality class.rnrnEvery month the stock manager of The Royal Pearl prepares a list with the number of pearls needed in each quality class. The pearls are bought on the local pearl market. Each quality class has its own price per pearl, but for every complete deal in a certain quality class one has to pay an extra amount of money equal to ten pearls in that class. This is to prevent tourists from buying just one pearl.rnrnAlso The Royal Pearl is suffering from the slow-down of the global economy. Therefore the company needs to be more efficient. The CFO (chief financial officer) has discovered that he can sometimes save money by buying pearls in a higher quality class than is actually needed. No customer will blame The Royal Pearl for putting better pearls in the bracelets, as long as the prices remain the same.rnrnFor example 5 pearls are needed in the 10 Euro category and 100 pearls are needed in the 20 Euro category. That will normally cost: (5+10)*10 + (100+10)*20 = 2350 Euro.rnrnBuying all 105 pearls in the 20 Euro category only costs: (5+100+10)*20 = 2300 Euro.rnrnThe problem is that it requires a lot of computing work before the CFO knows how many pearls can best be bought in a higher quality class. You are asked to help The Royal Pearl with a computer program.rnrnGiven a list with the number of pearls and the price per pearl in different quality classes, give the lowest possible price needed to buy everything on the list. Pearls can be bought in the requested, or in a higher quality class, but not in a lower one.rn rnrnInputrnThe first line of the input contains the number of test cases. Each test case starts with a line containing the number of categories c (1 <= c <= 100). Then, c lines follow, each with two numbers ai and pi. The first of these numbers is the number of pearls ai needed in a class (1 <= ai <= 1000). The second number is the price per pearl pi in that class (1 <= pi <= 1000). The qualities of the classes (and so the prices) are given in ascending order. All numbers in the input are integers.rn rnrnOutputrnFor each test case a single line containing a single number: the lowest possible price needed to buy everything on the list.rn rnrnSample Inputrn2rn2rn100 1rn100 2rn3rn1 10rn1 11rn100 12rn rnrnSample Outputrn330rn1344

没有更多推荐了,返回首页

私密
私密原因:
请选择设置私密原因
  • 广告
  • 抄袭
  • 版权
  • 政治
  • 色情
  • 无意义
  • 其他
其他原因:
120
出错啦
系统繁忙,请稍后再试

关闭