#define NDEBUG
#include <cassert>
#include <iostream>
using namespace std;
#define MAX_NUMBER 9
#define MAX_NODES 100
class Node{
public:
int y;
char x;
};
class Queue{
public:
Queue(){
head_ = -1;
tail_ = 0;
len_ = 0;
capacity_ = MAX_NODES+2;
pbuff = new Node [MAX_NODES+2];
}
~Queue(void){
delete[] pbuff;
}
int IsEmpty(void){
return len_ == 0;
}
int IsFull(void){
return len_ == capacity_;
}
void Enqueue(Node e){
pbuff[ tail_++ ] = e;
++len_;
if ( tail_ == capacity_ )
tail_ = 0;
}
Node Dequeue(void){
--len_;
if ( head_ == capacity_ )
head_ = -1;
return pbuff[ ++head_ ];
}
private:
int capacity_;
int len_;
int head_;
int tail_;
Node *pbuff;
};
char _index_tab[ 4 ] = {'A','B','C','D'};
Queue _queue_array[MAX_NUMBER];
Node _node_array[MAX_NODES];
int main(void){
int i,j;
int num;
Node node;
cin >> num;
for ( i=0; i<num; i++ ){
cin >> node.x >> node.y;
assert(node.y>=1&&node.y<=9);
_queue_array[ node.y-1 ].Enqueue(node);
}
// 保存并输出第一次进队出队结果
for ( i=j=0; i<MAX_NUMBER; i++ ){
cout << "Queue" << i+1 << ":";
while( !(_queue_array[i].IsEmpty()) ){
_node_array[ j ] = _queue_array[ i ].Dequeue();
cout << _node_array[ j ].x << _node_array[ j ].y << " ";
j++;
}
cout << endl;
}
assert(j==num);
for ( i=0; i<num; i++ ){
assert(_node_array[i].x>='A'&&_node_array[i].x<='D');
_queue_array[ _node_array[i].x-'A' ].Enqueue(_node_array[i]);
}
// 保存并输出第二次入队出队结果
for ( i=j=0; i<=3; i++ ){
cout << "Queue" << _index_tab[ i ] << ":" ;
while( !(_queue_array[i].IsEmpty()) ){
node = _queue_array[ i ].Dequeue();
_node_array[ j++ ] = node;
cout << node.x << node.y << " " ;
assert(j<=num);
}
cout << endl;
}
assert(j==num);
// 输出排序后结果
for ( i=0; i<num; i++ ){
cout << _node_array[ i ].x << _node_array[ i ].y << " ";
}
return 0;
}
POJ——用队列对扑克牌排序(AC)
最新推荐文章于 2020-08-19 22:54:54 发布