题意:给出 n张牌,从上往下编号依次为1到n,当牌的数目至少还剩下2张时,把第一张牌扔掉,然后把新的一张牌放在牌堆的最底部,问最后剩下的那一张牌是哪一张牌。
模拟队列的操作-------
1 #include<iostream> 2 #include<cstdio> 3 #include<cstring> 4 #include<algorithm> 5 #include<vector> 6 #include<queue> 7 using namespace std; 8 9 queue<int>q; 10 11 int main() 12 { 13 int i,j,n,ans,a,b; 14 while(scanf("%d",&n)!=EOF&&n) 15 { 16 while(!q.empty()) q.pop(); 17 for(i=1;i<=n;i++) 18 q.push(i); 19 printf("Discarded cards:"); 20 while(q.size()!=1) 21 { 22 b=q.front();q.pop(); 23 if(q.size()!=1) printf(" %d,",b); 24 else printf(" %d",b); 25 a=q.front();q.pop(); 26 q.push(a); 27 } 28 printf("\n"); 29 printf("Remaining card: %d\n",q.front()); 30 } 31 }
时间: 2024-10-16 09:54:24