POJ——用队列对扑克牌排序(AC)

#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;
}


  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值