地址:
点击打开链接
利用堆栈的性质实现数据反转,没什么技术,但是卡了半天,就是scanf的输入,最后都改成了cin
#include<iostream> #include<cstdio> #include<string.h> #include<stack> #include<queue> using namespace std ; #define MAX 105 struct Node{ int num ; char state ; }; Node nodes[MAX]; stack<Node> reverse_stack; queue<Node> reverse_queue ; void stackReverse(stack<Node>&from , queue<Node>&to ){ Node temp ; while(!from.empty()) { temp =from.top(); from.pop(); if(temp.state=='U') temp.state ='D' ; else temp.state ='U' ; to.push(temp); } while(!to.empty()) { from.push(to.front()); to.pop(); } } void trans(stack<Node>&from , stack<Node>&to) { int index = 1 ; while(!to.empty()) { reverse_stack.push(to.top()); to.pop(); } while(!from.empty()) { reverse_stack.push(from.top()); from.pop(); } while(!reverse_stack.empty()) { nodes[index++] = reverse_stack.top(); reverse_stack.pop(); } } int main(){ int Num,state ,i,j; char opt ; int case_num = 1 ; while(scanf("%d",&Num)&&Num!=0) { stack<Node> left_stack; stack<Node> right_stack; for(i= 1;i <=Num ; i ++ ) { cin>>nodes[i].state; nodes[i].num = i; } int from = 1, to = Num; left_stack.push(nodes[from]); right_stack.push(nodes[to]); while(from!=to-1) { cin >> opt; if(opt=='R') { to--; stackReverse(right_stack,reverse_queue); right_stack.push(nodes[to]); }else { from ++; stackReverse(left_stack,reverse_queue); left_stack.push(nodes[from]); } } cin >> opt; if(opt == 'R') { stackReverse(right_stack,reverse_queue); trans(right_stack,left_stack); } else { stackReverse(left_stack,reverse_queue); trans(left_stack,right_stack); } scanf("%d",&Num); int temp ; printf("Pile %d\n",case_num++); for(i = 1 ; i <= Num ; i ++) { scanf("%d",&temp); if(nodes[temp].state == 'U') printf("Card %d is a face up %d.\n",temp ,nodes[temp].num); else printf("Card %d is a face down %d.\n",temp ,nodes[temp].num); } } }