zoukankan      html  css  js  c++  java
  • 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;
    
    }

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


  • 相关阅读:
    记住我
    国米夺冠
    小谈“汉字转换成拼音(不带声调)”
    NLP资源共享盛宴
    文本分类资源和程序开源共享
    菜鸟进阶:C++实现Chisquare 特征词选择算法
    欢迎大家试用信息领域学科知识服务平台
    欢迎大家加入NLP,WEBIR,DATA Ming 的技术QQ群
    求两点之间所有路径的算法
    step by step 文本分类(一)
  • 原文地址:https://www.cnblogs.com/lj030/p/3002334.html
Copyright © 2011-2022 走看看