题意:Jerry、Tom玩游戏,给你出n个盒子,a[ i ]表示初始时,第i个中小球个数。之后Jerry可在每个盒子中加入0或k倍的小球,完成后,Jerry可以重排盒子的顺序,若能使第i个盒子中有i个小球,则Jerry获胜,输出“Jerry”,否则输出“Tom” 。
分析:首先统计每种数量的球有多少个盒子,然后从小到大分过去,剩下的盒子放到i+k的位置,这样扫描一遍数组,若有超过一个及一个以上数量的球不能找到对应的盒子则Tom赢,否则Jerry赢。
#include<iostream> using namespace std; int main() { int M,N,K,x,i; int cnt[105]; bool f; ios::sync_with_stdio(false); cin>>M; while(M--) { cin>>N>>K; memset(cnt,0,sizeof(cnt)); for(i=0;i<N;i++) cnt[cin>>x,x]++; f=true; for(i=1;i<=N;i++) { if(!cnt[i]) { f=false; break; } cnt[i+K]+=cnt[i]-1; } if(f) cout<<"Jerry"<<endl; else cout<<"Tom"<<endl; } return 0; }
时间: 2024-07-30 22:03:58