您的位置:首页 > 理论基础 > 数据结构算法

UVA 10935 Throwing cards away I 【数据结构】【模拟】

2017-01-14 21:49 411 查看
题目链接:https://vjudge.net/problem/UVA-10935

题目大意:

桌上有n张牌,从第一张牌开始,从上往下一次编号为1~n,当至少还剩下两张牌是进行以下操作:把第一张牌扔掉,然后把新的第一张牌放到整叠牌的最后,求扔掉的牌所组成的序列以及最后剩下的牌。

题目思路:

原来以为是模拟,直接上暴力,AC之后反倒想明白其实是一个队列…

致IQ == 0的我…

唯一需要注意的这么一组数据:

1

实际的输入应该是 

Discarded cards:

Remaining card: 1

其实这个漏洞我是从 udebug 中发现的,udebug这道题的第一个数据很精彩,给好评~

剩下的没什么了,就是纯模拟。

附代码:

#include <cstdio>
#include <cstring>
#include <iostream>

using namespace std;

const int size = 505;
int n, poker[size];

int main() {
// freopen("10935.out","w", stdout); (udebug)
while( scanf("%d", &n), n ) {
int beg = 1, end = n;
memset(poker, 0, sizeof(poker));

printf("Discarded cards:");
for ( int i = 1; i <= n; i++ ) poker[i] = i;
if( beg != end ) {
while( beg < end-1 ) {
printf(" %d,", poker[beg]);
poker[++ end] = poker[beg+1];
beg += 2;
// printf(" %d %d\n", beg, end);
}
printf(" %d", poker[beg]);
}

printf("\nRemaining card: %d\n", poker[end]);
}

return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: