0305:用队列对扑克牌排序
#include<iostream>
#include<stack>
#include<queue>
using namespace std;
void show(queue<string> Q) {
while (!Q.empty()) {
cout << Q.front()<<" ";
Q.pop();
}
cout << endl;
}
int main() {
int n = 0;
cin >> n;
string* strs = new string[n];
for (int i = 0; i < n; i++)
{
cin >> strs[i];
}
queue<string> q1[10], q2[4];
//q1
for (int i = 0; i < n; i++) {
q1[strs[i][1]-'0'].push(strs[i]);
}
for (int i = 1; i <= 9; i++) {
printf("Queue%d:", i);
while (!q1[i].empty()) {
cout << q1[i].front()<<" ";
q2[q1[i].front()[0] - 'A'].push(q1[i].front());
q1[i].pop();
}
cout << endl;
}
//q2
for (int i = 0; i <4; i++) {
printf("Queue%c:", i+'A');
show(q2[i]);
}
for (int i = 0; i < n; i++)
{
cout << strs[i]<<" ";
}
return 0;
}


思路
1.对于q1,通过判断字符串strs[i]的第二个字符确定要进哪个栈中,对strs[i][1]-'0'将字符转为数字1~9
2.可将q1中的元素直接出队再入队到q2中实现排序
3.通过判断字符串strs[i]的第一个字符确定要进q2的哪个栈中,对strs[i][0]-'A'将字符转为数字0~3

浙公网安备 33010602011771号