Description
Tom has N songs and he would like to record them into CDs. A single CD can contain at mostK 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
Output
For each test case, output the minimum number of CDs required, if the above constraints are satisfied.
Sample Input
Sample Output
Source
The 5th UESTC Programming Contest Preliminary
这样的题出来就是坑人的,看似很简单,可是一旦漏掉了某些特殊情况,提交时总是出现的“wrong anwser”就实在让人抓狂。。。
不急,慢慢分析:
⑴、K<13时,显然,如果N%K==0,那就是N/K个光盘;
如果N%K!=0,那就需要N/K+1个光盘。
⑵、K=13时,要当12来对待,同上:
如果N%12==0,那就是N/12个光盘;
如果N%12!=0,那就需要N/12+1个光盘。
K>13时,是不是如果N%K==13,就一定需要多加2个光盘呢,非也:
⑶、K=14时,如果N%K==0,那就是N/K个光盘;
如果N%K==13,那就是N/K+2个光盘;
如果N%K!=0、13;那就是N/K+1个光盘;
⑷、K>14时,如果N%K==0,那就是N/K个光盘;
(如果N%K==13,那就是N/K+2个光盘???NO,因为要光盘数最小,所以我们可以把任意一个前面的一张光盘里面的一首歌移到这张光盘上,这样就是14首,而被移走歌曲的那张光盘也不会剩余13首歌,因为K>14。。这种做法对于K=14就不适合了!!)
所以如果N%K!=0,那就是N/K+1个光盘;
好了,到这就结束了??如果提交程序的话,依然会是“wrong anwser”,到底少了神马咧?
⑸、如果TOM总共就13首歌,即N=13时,应用到K<13,K=13,K=14三种情况,都没问题。可是K>14时,按我们的思路,N%K==13!=0,那么就是N/K+1=1个光盘。可是这样的话,这个光盘上就会有13首歌,这时我就想了,难道不能把一首歌重复刻录两次,这样就是14首歌??可惜出题人并没有考虑到这点,我们也无法和服务器的评判标准对抗。。。。所以N=13,K>14时,就需要2个光盘。。。。
#include<iostream>
using namespace std;
int main()
{
int T;
cin>>T;
while(T--)
{
int N,K,cd;
cin>>N>>K;
if(K<13)
{
if(N%K==0)
cd=N/K;
else
cd=N/K+1;
}
else if(K==13)
{
if(N%12==0)
cd=N/12;
else
cd=N/12+1;
}
else if(K==14)
{
if(N%K==0)
cd=N/K;
else if(N%K==13)
cd=N/K+2;
else
cd=N/K+1;
}
else
{
if(N==13)
cd=2;
else if(N%K==0)
cd=N/K;
else
cd=N/K+1;
}
cout<<cd<<endl;
}
return 0;
}