UESTC 65 CD Making 贪心法

CD Making

Time Limit: 3000/1000MS (Java/Others)     Memory Limit: 65535/65535KB (Java/Others)
Submit  Status

Tom has  N  songs and he would like to record them into CDs. A single CD can contain at most  K  songs. In addition, Tom is very superstitious 

and he believes the number 13 would bring bad luck, so he will never let a CD contain exactly  13  songs. Tom wants to use as few CDs as 

possible to record all these songs. Please help him.

Input

There are  T  test cases. The first line gives  T , number of test cases.  T  lines follow, each contains  N  and  K , number of songs Tom wants to 

record into CDs, and the maximum number of songs a single CD can contain.

1N1000,1K1000

Output

For each test case, output the minimum number of CDs required, if the above constraints are satisfied.

Sample input and output

Sample Input Sample Output
2
5 2
13 13
3
2

Hint

If Tom has  5  songs to record and a single CD can contain up to  2  songs, Tom has to use  3  CDs at minimum, each contains  2 2 1  songs, 

respectively.

In the second case, Tom wants to record  13  songs, and a single CD can hold  13  songs at most. He would have been able to use only  1  CD 

if 

he were not so superstitious. However, since he will not record exactly  13  songs into a single CD, he has to use  2  CDs at least, the first 

contains 


12
 songs and the second contains one(Other solutions to achieve  2  CDs are possible, such as ( 11 2 ), ( 10 3 ), etc.).

Source

The 5th UESTC Programming Contest Preliminary

My Solution

秒杀题,就是那个K==14的时候要想到,要额外处理

#include <iostream>
#include <cstdio>
using namespace std;

int main()
{
    int T,N,K,ans;
    scanf("%d",&T);
    while(T--){
        ans=0;
        scanf("%d%d",&N,&K);
        if(N<=K) {if(N!=13)printf("1"); else printf("2");}
        else {
            if(K==13) {ans=N/12;if(N%12!=0) ans+=1;}
            else if(K<13||K>14) {
                ans=N/K; if(N%K!=0) ans+=1;         //如果K>14&&N%K==13,只要从另外一个地方补个过来变成14就可以了,不用另外加CD
            }
            else {                                  //K==14
                ans=N/K;
                if(N%K!=0){
                    if(N%K!=13) ans+=1;
                    else ans+=2;
                }
            }
            printf("%d",ans);
        }
        if(T) printf("\n");
    }
    return 0;
}



谢谢

你可能感兴趣的:(ACM,ICPC,OJ,uestc,思维题,分类讨论和注意关键位置)