POJ 1214 解题报告

#include <iostream>
#include <string>
#include <stack>
using namespace std;
const int maxn=53;
int mount;
stack<string> s[maxn];
void stackInput();
void moveCard();
void finalOutput();
bool isMatch(string s1,string s2);
int main()
{
	string str;
	while(cin >> str,str!="#")
	{
		s[0].push(str);
		stackInput();
		moveCard();
		finalOutput();
	}
	return 0;
}
void stackInput()
{
	mount=52;
	string str;
	for(int i=1;i<mount;i++)
	{
		cin >> str;
		s[i].push(str);//把输入的数据压入栈中
	}
}
void moveCard()//模拟纸牌的移动
{
	int i;

	while(1)
	{
	for(i=0;i<mount;i++)
	{
		if(i>2&&isMatch(s[i].top(),s[i-3].top()))
		{
			s[i-3].push(s[i].top());
			s[i].pop();
			break;
		}
		if(i>0&&isMatch(s[i].top(),s[i-1].top()))
		{
			s[i-1].push(s[i].top());
			s[i].pop();
			break;
		}
	}
	if(i==mount) break;//栈数组已遍历完
	if(s[i].empty())
	{
		int k;
		for( k=i;k<mount-1;k++)
		{
			s[k]=s[k+1];//栈也可以直接复制
		}
		while(!s[k].empty()) s[k].pop();//此部很关键,清空不需要的栈以免对下一组数据造成影响
		mount--;
	}
	}
}
bool isMatch(string s1,string s2)
{
	if(s1[0]==s2[0]||s1[1]==s2[1]) return true;
	else return false;
}
void finalOutput()
{
	cout << mount << " piles remaining:";
	for(int j=0;j<mount;j++)
	{
		cout << " " << s[j].size();
		while(!s[j].empty()) s[j].pop();//此步很关键,把栈清空以免对下组数据造成影响
	}
	cout << endl;

}

这道题所用知识就是栈和模拟,用了很长时间才把题读懂,题目不是很难,但对于我这个菜鸟来说,用了很长的时间

转载于:https://www.cnblogs.com/lj-vs-lishimin/archive/2012/05/18/2774412.html

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值