zoukankan      html  css  js  c++  java
  • 例3.1 猜猜数据结构 UVa11995

    1.标题叙述性说明:点击打开链接

    2.解题思路:据来推測一种可能的数据结构,备选答案有“栈,队列。优先队列”。结果也可能都不是或者不确定。

    STL中已经有这三种数据结构了,因此直接模拟题意,输出时推断是否相应就可以。注意:弹出时要推断一下是否已经为空。

    3.代码:

    #define _CRT_SECURE_NO_WARNINGS 
    #include<iostream>
    #include<algorithm>
    #include<string>
    #include<sstream>
    #include<set>
    #include<vector>
    #include<stack>
    #include<map>
    #include<queue>
    #include<deque>
    #include<cstdlib>
    #include<cstdio>
    #include<cstring>
    #include<cmath>
    #include<ctime>
    #include<functional>
    using namespace std;
    
    stack<int>s;
    queue<int>q;
    priority_queue<int>p;
    char st[][20] = { "impossible", "priority queue", "queue","not sure" , "stack", "not sure","not sure" ,"not sure"  };
    void clear()
    {
    	while (!s.empty())s.pop();
    	while (!q.empty())q.pop();
    	while (!p.empty())p.pop();
    }
    void add(int x)
    {
    	s.push(x);
    	q.push(x);
    	p.push(x);
    }
    void pop()
    {
    	if (!s.empty())s.pop();
    	if (!q.empty())q.pop();
    	if (!p.empty())p.pop();
    }
    int main()
    {
    	//freopen("t.txt", "r", stdin);
    	int n;
    	while (~scanf("%d", &n))
    	{
    		clear();
    		int ok1, ok2, ok3;
    		ok1 = ok2 = ok3 = 1;
    		for (int i = 0; i < n; i++)
    		{
    			int a, b;
    			cin >> a >> b;
    			if (a == 1)add(b);//统一加入
    			else
    			{
    				if (s.empty() || s.top() != b)
    					ok1 = 0;
    				if (q.empty() || q.front() != b)
    					ok2 = 0;
    				if (p.empty() || p.top() != b)
    					ok3 = 0;
    				pop();//统一弹出
    			}
    		}
    		int x = (ok1 << 2) | (ok2 << 1) | ok3;//编码,方便输出结果
    		printf("%s
    ", st[x]);
    	}
    	return 0;
    }

    版权声明:本文博客原创文章。博客,未经同意,不得转载。

  • 相关阅读:
    python自动生成小学四则运算题目
    软件工程第一章心得体会
    Python微信机器人
    利用python进行微信好友分析
    python操作数据库读书笔记
    初学爬虫之访问goole网页与爬取中国大学排名。
    python之预测体育竞技分析
    5、用python写一个自己的网页
    用turtle实现动态汉诺塔
    面向对象与正则表达式的学习(自动更正,和代数运算)
  • 原文地址:https://www.cnblogs.com/hrhguanli/p/4686685.html
Copyright © 2011-2022 走看看