小猫钓鱼都知道怎么玩吧,那怎么用代码表示呢?其实用C++的队列和栈的知识很容易实现,下面展示一下我的代码(该代码规则是完全按照q1先出q2后出的规矩,不管是否收牌)
#include#include #include using namespace std; int a[11]; int main() { for(int i=1;i<=10;i++) { a[i]=0; } stack s; queue q1,q2; int n; cin>>n; int z,m,t; for(int i=1;i<=n;i++) { cin>>z; q1.push(z); } for(int i=1;i<=n;i++) { cin>>z; q2.push(z); } while(q1.size()!=0&&q2.size()!=0) { t=q1.front(); if(a[t]==1) { q1.pop(); q1.push(t); a[t]=0; while(s.top()!=t) { m=s.top(); q1.push(m); s.pop(); a[m]=0; } m=s.top(); q1.push(m); s.pop(); a[m]=0; } else { q1.pop(); s.push(t); a[t]=1; } if(q1.empty()==1) { break; } t=q2.front(); if(a[t]==1) { q2.pop(); q2.push(t); a[t]=0; while(s.top()!=t) { m=s.top(); q2.push(m); s.pop(); a[m]=0; } m=s.top(); q2.push(m); s.pop(); a[m]=0; } else { q2.pop(); s.push(t); a[t]=1; } } if(q1.size()!=0) { cout<<"q1获胜,手里的牌有"< 哪里不对欢迎指教。
(祖国必然统一!!!)



