南邮 OJ 1097 卡片游戏

来源:互联网 发布:nginx rewrite last 编辑:程序博客网 时间:2024/05/16 17:34

卡片游戏

时间限制(普通/Java) : 2000 MS/ 6000 MS          运行内存限制 : 65536 KByte
总提交 : 1372            测试通过 : 268 

比赛描述

桌上有一叠牌,从第一张牌(即位于顶面的牌)开始从上往下依次编号为1~n。当至少还剩两张牌时进行以下操作:把第一张扔掉,然后把新的第一张放到整叠牌的最后。



输入

输入n,n≤1000000。

输出

输出每次扔掉的牌,以及最后剩下的牌。

样例输入

7

样例输出

1 3 5 7 4 2 6

题目来源

刘汝佳《算法竞赛入门经典》,编自UVa10935 Throwing cards away I




#include<iostream>using namespace std;struct link_node{long i;link_node* next;};int main(){long n,i;link_node *head,*tail,*p;cin>>n;if(n==1){cout<<1<<endl;return 0;}if(n==2){cout<<1<<" "<<2<<endl;return 0;}head = new link_node();head->i = 1;tail = head;for(i=2;i<=n;++i){p = new link_node();p->i = i;tail->next = p;tail = p;}while(head->next!=tail){cout<<head->i<<" ";p = head;head = head->next;delete p;p = head;head = head->next;tail->next = p;tail = p;}cout<<head->i<<" "<<tail->i<<endl;}





0 0
原创粉丝点击