当前位置: 代码迷 >> 综合 >> uva 10935 Throwing cards away I
  详细解决方案

uva 10935 Throwing cards away I

热度:98   发布时间:2023-12-06 08:48:33.0

题目:Throwing cards away I


题意:输入整数n,表示有n张卡牌,从上到下表示为1~n。每一次丢掉最上面哒的张卡牌,再把此时点的最上面一张插到最下面。输出丢掉卡牌的顺序以及剩余的卡牌。


思路:模拟。手写链表存储。


注意:

1、当n=1时单独讨论。

2、犯了一个低级错误,结构体的构造函数没有写全。


代码:

#include<cstdio>
#include<iostream>
#include<string>
#include<vector>
#include<set>
#include<map>
#include<algorithm>
#include<cmath>
using namespace std;struct Card {bool now;int before,after;Card() {}Card(int one,bool two,int three) {before=one,now=two,after=three;}
};int n;
Card a[100];void init() {for(int i=1; i<=n; i++) {a[i]=Card(i-1,true,i+1);}a[1].before=-1;a[n].after=-1;
}int main() {while(scanf("%d",&n)==1&&n!=0) {if(n==1) {printf("Discarded cards:\n");printf("Remaining card: 1\n");continue;}init();vector<int> Throw;for(int i=1; i<n; i++) {int Top;for(int j=1; j<=n; j++) {if(a[j].before==-1&&a[j].now==true) {Throw.push_back(j);a[j].now=false;Top=a[j].after;break;}}for(int j=1; j<=n; j++) {if(a[j].after==-1) {a[a[Top].after].before=-1;a[j].after=Top;a[Top].before=j;a[Top].after=-1;}}}printf("Discarded cards: ");for(int i=0; i<Throw.size()-1; i++) {printf("%d, ",Throw[i]);}printf("%d\n",Throw[Throw.size()-1]);int Remain;for(int i=1; i<=n; i++) {if(a[i].now==true) Remain=i;}printf("Remaining card: %d\n",Remain);}return 0;
}


  相关解决方案