我在网上看了其他博主写的代码,看不太明白,就自己写了下,可参考以下链接
https://blog.csdn.net/synapse7/article/details/9698173?utm_source=miniapp_weixin
2406:Card Stacking
查看
提交
统计
提问
总时间限制: 20000ms 单个测试点时间限制: 1000ms 内存限制: 65536kB描述Bessie is playing a card game with her N-1 (2 <= N <= 100) cow friends using a deck with K (N <= K <= 100,000; K is a multiple of N) cards. The deck contains M = K/N "good" cards and K-M "bad" cards. Bessie is the dealer and, naturally, wants to deal herself all of the "good" cards. She loves winning.
Her friends suspect that she will cheat, though, so they devise a dealing system in an attempt to prevent Bessie from cheating. They tell her to deal as follows:
1. Start by dealing the card on the top of the deck to the cow to her right
2. Every time she deals a card, she must place the next P (1 <= P <= 10) cards on the bottom of the deck; and
3. Continue dealing in this manner to each player sequentially in a counterclockwise manner
Bessie, desperate to win, asks you to help her figure out where she should put the "good" cards so that she gets all of them. Notationally, the top card is card #1, next card is #2, and so on.
输入* Line 1: Three space-separated integers: N, K, and P输出* Lines 1..M: Positions from top in ascending order in which Bessie should place "good" cards, such that when dealt, Bessie will obtain all good cards.
#include<iostream>
#include<queue>
#include<algorithm>
using namespace std;
const int N=100010;
int main()
{
queue<int>q;
int a[N];//用来存放发到Bessie手中的牌
int n,k,p,cnt=1,sum=0;//sum用来存女主人公手中的牌数
cin>>n>>k>>p;
for(int i=1;i<=k;i++){//遍历k张牌
q.push(i);//把p张牌入队
// cout<<"push:"<<i;
}
//printf("\n");
while(!q.empty()){
if(sum==k/n)//一定要写,不然会出现run time error
break;
if(cnt%n==0){//牌发到Bessie手中时
a[sum]=q.front();
//cout<<"a:sum="<<a[sum]<<endl;
sum++;
}
q.pop();
for(int j=1;j<=p;j++)
{
int b=q.front();
q.pop();
q.push(b);//每发一次牌将p张牌压底
//cout<<"push:"<<b;
}
cnt++;
//cout<<"cnt="<<cnt;
//printf("\n");
}
sort(a,a+sum);//排序
for(int i=0;i<sum;i++)
{
cout<<a[i]<<endl;
}
return 0;
}