标签:
#include <iostream>
#include <string.h>
using namespace std;
int main()
{
int n;
while(cin>>n&&n)
{
int a[100],b[100],c[1];
int i,j=0,head,tail;
head=0; //将head标记到队列开头
tail=n; //将tail标记到队列最后一位的后一位,因为需要留一个位置给开头的数
for(i=0; i<n; i++)
a[i]=i+1;// 1 2 3 4 5 6 7
if(n==1)
{
cout<<"Discarded cards:"<<endl;
cout<<"Remaining card: 1"<<endl;
}
else
{
while(head+1<tail) //这里的判断条件表示当a数组还没有到一个数时,就循环下去
{
b[j++]=a[head]; //将第一个数赋给数组b,并且j由0开始增加
head++; //head向后移动一位,此时去掉了”第一个数“ 2 3 4 5 6 7
a[tail]=a[head]; //将第二个数移动到队列最后 2 3 4 5 6 7 2
c[0]=a[tail]; //每次将最后一个数赋给长度为1的数组c,当还剩最后一个数时,c[0]就是剩下的数
tail++; //tail向后移动一位,为下一个移动到队尾的数留位子
head++; //因为已经将队列的第一个移动到了队尾,所以head向后移动一位 3 4 5 6 7 2
}
cout<<"Discarded cards: ";
for(int k=0; k<=j-2; k++) //因为j++。当跳出while时,j还多加了1,所以多减个1
cout<<b[k]<<", ";
cout<<b[j-1]<<endl; //与上一条一样
cout<<"Remaining card: ";
cout<<c[0]<<endl;
}
}
}
特别注意输出是否少多空格,符号问题,反正我是吃了苦头.....
标签:
原文地址:http://www.cnblogs.com/huangguodong/p/4655518.html