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

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


  • 相关阅读:
    Linux centos 安装 Node.js
    maven 常用命令
    linux centos 设置笔记本合盖不待机
    linux centos 网卡有关调试
    Linux centos 安装 maven 3.5.4
    Linux centos 安装 jenkins & 本地构建jar & 远程构建jar
    Linux centos 安装 tomcat 7
    Linux centos 安装 JDK 8
    JS正则 replace()方法全局替换变量(可以对变量进行全文替换)
    node:json与csv互转
  • 原文地址:https://www.cnblogs.com/lj030/p/3002334.html
Copyright © 2011-2022 走看看