|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
- #include<stdio.h>
- struct queue{
- int data[1000];
- int head;
- int tail;
- };
- struct stack{
- int data[10];
- int top;
- };
- int main(){
- struct queue q1,q2;
- struct stack s;
- int book[10]={0};
-
- //初始化队列
- q1.head=0;q1.tail=0;
- q2.head=0;q2.tail=0;
- //初始化栈
- s.top=0;
- //手上的6张牌
- for(int i=0;i<6;i++){
- scanf("%d",&q1.data[q1.tail++]);
- }
- for(int i=0;i<6;i++){
- scanf("%d",&q1.data[q1.tail++]);
- }
- //出牌
- int t;
- while(q1.head<q1.tail&&q2.head<q2.tail){
- t=q1.data[q1.head];
- //打出牌
- q1.head++;
- //1没赢
- if(book[t]==0){
- s.data[++s.top]=t;
- book[t]=1;
- }
- //1可以赢
- else{
- q1.data[q1.tail++]=t;
- while(s.data[s.top]!=t){
- book[s.data[s.top]]=0;
- q1.data[q1.tail++]=s.data[s.top--];
- }
- }
-
- t=q2.data[q2.head];
- //打出牌
- q2.head++;
- //2没赢
- if(book[t]==0){
- s.data[++s.top]=t;
- book[t]=1;
- }
- //2可以赢
- else{
- q2.data[q2.tail++]=t;
- while(s.data[s.top]!=t){
- book[s.data[s.top]]=0;
- q2.data[q2.tail++]=s.data[s.top--];
- }
- }
- }
- if(q2.head==q2.tail){
- printf("q1赢了,他手上的牌是:");
- for(int i=q1.head;i<q1.tail;i++){
- printf(" %d",q1.data[i]);
- }
- if(s.top>0){
- printf("\n桌上的数为:");
- for(int i=1;i<=s.top;i++){
- printf(" %d",s.data[i]);
- }
- }else{
- printf("\n桌上无牌");
- }
- }else{
- printf("q2赢了,他手上的牌是:");
- for(int i=q2.head;i<q2.tail;i++){
- printf(" %d",q2.data[i]);
- }
- if(s.top>0){
- printf("\n桌上的数为:");
- for(int i=0;i<=s.top;i++){
- printf(" %d",s.data[i]);
- }
- }else{
- printf("\n桌上无牌");
- }
- }
- }
复制代码
这段代码中存在一些问题。首先,队列q2没有被正确初始化,其头尾指针应该也被初始化为0。其次,出牌的逻辑中存在错误,应该是先出牌再判断胜负,而不是先判断胜负再出牌。最后,打印桌上的牌时,应该从1开始遍历栈中的数据,而不是从0开始。
以下是修改后的代码:
- #include<stdio.h>
- struct queue{
- int data[1000];
- int head;
- int tail;
- };
- struct stack{
- int data[10];
- int top;
- };
- int main(){
- struct queue q1,q2;
- struct stack s;
- int book[10]={0};
-
- //初始化队列
- q1.head=0;q1.tail=0;
- q2.head=0;q2.tail=0;
- //初始化栈
- s.top=0;
- //手上的6张牌
- for(int i=0;i<6;i++){
- scanf("%d",&q1.data[q1.tail++]);
- }
- for(int i=0;i<6;i++){
- scanf("%d",&q2.data[q2.tail++]);
- }
- //出牌
- int t;
- while(q1.head<q1.tail&&q2.head<q2.tail){
- t=q1.data[q1.head];
- q1.head++;
- if(book[t]==0){
- s.data[++s.top]=t;
- book[t]=1;
- }
- else{
- q1.data[q1.tail++]=t;
- while(s.data[s.top]!=t){
- book[s.data[s.top]]=0;
- q1.data[q1.tail++]=s.data[s.top--];
- }
- }
- t=q2.data[q2.head];
- q2.head++;
- if(book[t]==0){
- s.data[++s.top]=t;
- book[t]=1;
- }
- else{
- q2.data[q2.tail++]=t;
- while(s.data[s.top]!=t){
- book[s.data[s.top]]=0;
- q2.data[q2.tail++]=s.data[s.top--];
- }
- }
- }
- if(q2.head==q2.tail){
- printf("q1赢了,他手上的牌是:");
- for(int i=q1.head;i<q1.tail;i++){
- printf(" %d",q1.data[ i]);
- }
- if(s.top>0){
- printf("\n桌上的数为:");
- for(int i=1;i<=s.top;i++){
- printf(" %d",s.data[ i]);
- }
- }else{
- printf("\n桌上无牌");
- }
- }else{
- printf("q2赢了,他手上的牌是:");
- for(int i=q2.head;i<q2.tail;i++){
- printf(" %d",q2.data[ i]);
- }
- if(s.top>0){
- printf("\n桌上的数为:");
- for(int i=1;i<=s.top;i++){
- printf(" %d",s.data[ i]);
- }
- }else{
- printf("\n桌上无牌");
- }
- }
- }
复制代码
|
|