大模拟,没什么好说的
为了简化操作,运用c++STL
对于牌堆,明显可以用队列模拟
对于玩家的牌,用vector储存,出牌可直接调用库函数
对牌单独定义结构体吗,方便操作
对于名字,不要打一堆if
尽量所有函数式编程,尽量复用代码,缩短代码长度,减少调试难度
以下给出2个代码
std
#include<bits/stdc++.h> using namespace std; struct card{ string col; string pattern; card(){}; card(string a,string b): col(a),pattern(b){}; }consult; int n,Round,direction=1; string name[5]={"xiran","sanqiu","shixin"}; queue<card> pai; vector<card> v[5]; void get_card(int x,int ad) { while(ad--){ if(pai.size()==0){ puts("There are no cards to take");//无牌可拿 for(int i=0;i<3;i++){ for(int j=0;j<v[i].size();j++) cout<<v[i][j].col<<" "<<v[i][j].pattern<<" "; puts(""); } exit(0); } v[x].push_back(pai.front()); pai.pop(); } } void next(int x){Round=((Round+x*direction)%3+3)%3;} void discard(int i) { card k= v[Round][i]; consult=k;//参考牌替换 pai.push(k);//加入牌堆 v[Round].erase(v[Round].begin()+i,v[Round].begin()+i+1);//删除此玩家的手牌 if(k.pattern=="Skip"){next(2);} else if(k.pattern=="Reverse"){direction=-direction;next(1);} else if(k.pattern=="Draw_Two"){next(1);get_card(Round,2);next(1);} else{next(1);} } void work() { for(int i=0;i<v[Round].size();i++) if(v[Round][i].col==consult.col||v[Round][i].pattern==consult.pattern){ discard(i); return; } //无牌可出 while(1){ get_card(Round,1);// 获得一张牌 int k=v[Round].size()-1; if(v[Round][k].col==consult.col||v[Round][k].pattern==consult.pattern){ discard(k); return; } } } int main() { consult=card("g","1");//规定初始参考牌为 绿色的1 cin>>n; for(int i=1;i<=n;i++){ string a,b; cin>>a>>b; pai.push( card(a,b) );//加入牌堆 } for(int i=0;i<3;i++) get_card(i,7);//每人获得7张的初始牌 while(v[0].size()&&v[1].size()&&v[2].size()){ work(); } for(int i=0;i<3;i++) if(!v[i].size()) cout<<name[i]<<"\n"; for(int i=0;i<3;i++){ for(int j=0;j<v[i].size();j++) cout<<v[i][j].col<<" "<<v[i][j].pattern<<" "; puts(""); } return 0; }
审题人代码
#include<iostream> #include<cstdio> #include<algorithm> #include<cstring> using namespace std; struct node{ char col; string act; }card[4][10005]; node allofcard[10005]; bool aim[4][10005]; int nowcard[4]; int leacard[4]; void print() { for(int j=1;j<=nowcard[1];j++) { if(aim[1][j]==0) cout<<card[1][j].col<<" "<<card[1][j].act<<" "; } cout<<endl; for(int j=1;j<=nowcard[2];j++) { if(aim[2][j]==0) cout<<card[2][j].col<<" "<<card[2][j].act<<" "; } cout<<endl; for(int j=1;j<=nowcard[3];j++) { if(aim[3][j]==0) cout<<card[3][j].col<<" "<<card[3][j].act<<" "; } } int main() { int n; scanf("%d",&n); int cnt=0; int now=1; for(int i=1;i<=n;i++) { if(now==4) { cin>>allofcard[++cnt].col; getchar(); cin>>allofcard[cnt].act; } else { cin>>card[now][++cnt].col; getchar(); cin>>card[now][cnt].act; nowcard[now]++; if(cnt==7) now++,cnt=0; } } if(now<=3) { printf("There are no cards to take\n"); print(); return 0; } for(int i=1;i<=3;i++) leacard[i]=nowcard[i]; int order=1; char stacol='g'; string stact="1"; int peo=1; int pointo=1; // cout<<"——————————————————————————————"<<endl; while(1) { bool flag=false; for(int i=1;i<=nowcard[peo];i++) { if(!aim[peo][i]&&(card[peo][i].col==stacol||card[peo][i].act==stact)) { aim[peo][i]=1; leacard[peo]--; flag=true; stacol=card[peo][i].col; stact=card[peo][i].act; // cout<<stacol<<" "<<stact<<endl; allofcard[++cnt].col=stacol; allofcard[cnt].act=stact; if(leacard[peo]==0) { if(stact=="Draw_Two") { int now1=peo; now1+=order; if(now1>3) now1=1; if(now1<1) now1=3; if(pointo>cnt) { printf("There are no cards to take\n"); print(); return 0; } card[now1][++nowcard[now1]]=allofcard[pointo++]; leacard[now1]++; if(pointo>cnt) { printf("There are no cards to take\n"); print(); return 0; } leacard[now1]++; card[now1][++nowcard[now1]]=allofcard[pointo++]; } if(peo==1) cout<<"xiran"<<endl; else if(peo==2) cout<<"sanqiu"<<endl; else cout<<"shixin"<<endl; print(); return 0; } if(stact=="Skip") { peo+=order; if(peo>3) peo=1; if(peo<1) peo=3; } else if(stact=="Reverse") { order*=-1; } else if(stact=="Draw_Two") { peo+=order; if(peo>3) peo=1; if(peo<1) peo=3; if(pointo>cnt) { printf("There are no cards to take\n"); print(); return 0; } card[peo][++nowcard[peo]]=allofcard[pointo++]; leacard[peo]++; if(pointo>cnt) { printf("There are no cards to take\n"); print(); return 0; } leacard[peo]++; card[peo][++nowcard[peo]]=allofcard[pointo++]; } break; } } if(leacard[peo]==0) { if(stact=="Draw_Two") { int now1=peo; now1+=order; if(now1>3) now1=1; if(now1<1) now1=3; if(pointo>cnt) { printf("There are no cards to take\n"); print(); return 0; } card[now1][++nowcard[now1]]=allofcard[pointo++]; leacard[now1]++; if(pointo>cnt) { printf("There are no cards to take\n"); print(); return 0; } leacard[now1]++; card[now1][++nowcard[now1]]=allofcard[pointo++]; } if(peo==1) cout<<"xiran"<<endl; else if(peo==2) cout<<"sanqiu"<<endl; else cout<<"shixin"<<endl; print(); return 0; } if(!flag) { if(pointo>cnt) { printf("There are no cards to take\n"); print(); return 0; } while(!(allofcard[pointo].col==stacol||allofcard[pointo].act==stact)) { card[peo][++nowcard[peo]]=allofcard[pointo++]; leacard[peo]++; if(pointo>cnt) { printf("There are no cards to take\n"); print(); return 0; } } stacol=allofcard[pointo].col; stact=allofcard[pointo].act; pointo++; allofcard[++cnt].col=stacol; allofcard[cnt].act=stact; // cout<<stacol<<" "<<stact<<endl; if(stact=="Skip") { peo+=order; if(peo>3) peo=1; if(peo<1) peo=3; } else if(stact=="Reverse") { order*=-1; } else if(stact=="Draw_Two") { peo+=order; if(peo>3) peo=1; if(peo<1) peo=3; if(pointo>cnt) { printf("There are no cards to take\n"); print(); return 0; } card[peo][++nowcard[peo]]=allofcard[pointo++]; leacard[peo]++; if(pointo>cnt) { printf("There are no cards to take\n"); print(); return 0; } leacard[peo]++; card[peo][++nowcard[peo]]=allofcard[pointo++]; } } peo+=order; if(peo>3) peo=1; if(peo<1) peo=3; } }