您的位置:首页 > 其它

UVa 10935 - Throwing cards away I

2013-05-03 15:13 274 查看
  《算法竞赛入门经典》6.1的题目。题目大意:给n张牌,放成一叠,从上到下编号从1到n,当至少还有两张牌时,丢弃最上面的牌,然后把新的最上面的牌放到最下面,一直重复,直到只剩下一张牌,输出丢弃牌的序列。

  用队列进行模拟,不过第一次提交的时候PE了,格式说明一下:

”Discarded cards:“,然后每一数字前加一个空格,第一个数字之后的数字要在空格前加逗号。代码如下:

View Code

#include <cstdio>
#include <cstdio>
#include <queue>
using namespace std;

const int maxn = 60;
queue<int> q;

int main()
{
#ifdef LOCAL
freopen("in", "r", stdin);
#endif
int n;
int ans[maxn];
while(scanf("%d", &n) != EOF && n)
{
for(int i = 1; i <= n; i++)   q.push(i);
int k = 0;
while(!q.empty())
{
ans[k++] = q.front();
q.pop();
int t = q.front();
q.pop();
q.push(t);
}
printf("Discarded cards:");
for(int i = 0; i < n-1; i++)
{
if(i)   printf(",");
printf(" %d", ans[i]);
}
printf("\nRemaining card: %d\n", ans[n-1]);
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: