直接用STL里的queue模拟即可。
1 #include <cstdio> 2 #include <queue> 3 using namespace std; 4 5 const int maxn = 60; 6 int discarded[maxn], cnt; 7 8 int main() 9 { 10 int n; 11 while(scanf("%d", &n) == 1 && n) 12 { 13 cnt = 0; 14 queue<int> Q; 15 for(int i = 1; i <= n; i++) Q.push(i); 16 while(Q.size() >= 2) 17 { 18 discarded[cnt++] = Q.front(); Q.pop(); 19 int t = Q.front(); Q.pop(); Q.push(t); 20 } 21 printf("Discarded cards:"); 22 for(int i = 0; i < cnt; i++) 23 { 24 if(i) printf(","); 25 printf(" %d", discarded[i]); 26 } 27 printf("\n"); 28 printf("Remaining card: %d\n", Q.front()); 29 } 30 31 return 0; 32 }
代码君
时间: 2024-11-03 10:19:36