卡片问题
题目算蛮好懂得 看图大概就可以知道题意
将两堆卡片的最上面拿一张拿起来相比,若A堆上的那一张比B堆小,则将两张卡片由小到大放入B堆的底部 ,若A堆上的那一张比B堆大,则反之.
这题我是直接用queue实作
比较快也比较好懂(虽然还没想到第二种方法的说XD
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include<iostream> | |
#include<queue> | |
using namespace std; | |
int main(){ | |
queue<int> q1; | |
queue<int> q2; | |
int n,x,y; | |
cin>>n;//总卡片数量 直接读掉就好 | |
cin>>n; | |
for(int i=0;i<n;i++){ | |
cin>>x; | |
q1.push(x); | |
} | |
cin>>n; | |
for(int i=0;i<n;i++){ | |
cin>>x; | |
q2.push(x); | |
} | |
int times = 0; | |
while(!q1.empty()&&!q2.empty()&×<10000){ | |
//取出第一个数相比较 并pop掉 | |
x = q1.front(); | |
y = q2.front(); | |
q1.pop(); | |
q2.pop(); | |
//将两数依小到大存入比较大的数的queue | |
if(x<=y){ | |
q2.push(x); | |
q2.push(y); | |
} | |
else{ | |
q1.push(y); | |
q1.push(x); | |
} | |
//总比较次数++ 进入无穷回圈则跳出 | |
times++; | |
} | |
if(times>=10000) cout<<"-1"<<endl; | |
else { | |
cout<<times; | |
if(!q2.empty()) cout<<" 2"<<endl; | |
else cout<<" 1"<<endl; | |
} | |
} |
沒有留言:
張貼留言
注意:只有此網誌的成員可以留言。