UVA-10935 Throwing cards away I
1657 ワード
/*
BTW, , , ,
:
http://www.cnblogs.com/AlgoWing/archive/2013/03/04/3189616.html
http://blog.csdn.net/mobius_strip/article/details/45157587
C++ blog:
http://www.cnblogs.com/xuning/p/3321733.html
( , size int , , ...)
*/
// :
#include
using namespace std;
const int maxn = 100000;
int Q[maxn];
int main()
{
int n;
while (cin >> n && n)
{
for (int i = 1; i <= n; i++) Q[i] = i;
int head = 1, tail = n, first = 1;
cout << "Discarded cards:"; //
while (head < tail)
{
if (!first) cout << ",";
cout << " " << Q[head];
head++;
Q[++tail] = Q[head++];
first = 0; // ,
}
cout << endl << "Remaining card: " << Q[head] << endl;
}
return 0;
}
// :
#include
#include
using namespace std;
queue Q;
int n;
int main()
{
while (cin >> n && n)
{
for (int i = 1; i <= n; i++) Q.push(i);
cout << "Discarded cards:";
while ((int)Q.size() > 1)
{
if ((int)Q.size() > 2)
{
cout << " " << Q.front() << ",";
Q.pop();
Q.push(Q.front());
Q.pop();
}
else
{
cout << " " << Q.front();
Q.pop();
}
}
cout << endl << "Remaining card: " << Q.front() << endl;
Q.pop();
}
return 0;
}