Uva 11995 - I Can Guess the Data Structure! (判断数据类型)
2013-11-27 00:19
696 查看
题目链接:
http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=229&page=show_problem&problem=3146
题目分析:
先设定一个结构体数组存储输入的数据,在定义三个函数判断是否为栈,队列,优先队列,再对返回值进行判断。
另外,在结构体里面定义友元函数,用来优先队列实现里面判断大小,注意,参数列表不能用 data &a, data &b会报错
代码:
用while(scanf("%d", &n))的话会超时 1.00s
用while(~scanf("%d", &n))的话只要 0.04
差距~~~~(>_<)~~~~
http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=229&page=show_problem&problem=3146
题目分析:
先设定一个结构体数组存储输入的数据,在定义三个函数判断是否为栈,队列,优先队列,再对返回值进行判断。
另外,在结构体里面定义友元函数,用来优先队列实现里面判断大小,注意,参数列表不能用 data &a, data &b会报错
代码:
#include<iostream> #include<stack> #include<queue> #include<stdio.h> #include<string.h> using namespace std; struct Data { int k, v; friend bool operator < (Data a, Data b){ return a.v < b.v; } }d[1010]; int n; int fstack(){ stack<int> s; for (int i = 0; i < n; ++i) { if (d[i].k == 1) s.push(d[i].v); if (d[i].k == 2) { if (s.empty() == 1) return 0; if (s.top() == d[i].v) { s.pop(); continue; } else return 0; } } return 1; } int fqueue(){ queue<int> qq; for (int i = 0; i < n; ++i) { if (d[i].k == 1) qq.push(d[i].v); if (d[i].k == 2) { if (qq.empty() == 1) return 0; if (qq.front() == d[i].v) { qq.pop(); continue; } else return 0; } } return 1; } int fpriqueue(){ priority_queue<Data> qq; for (int i = 0; i < n; ++i) { if (d[i].k == 1) qq.push(d[i]); if (d[i].k == 2) { if (qq.empty() == 1) return 0; if (qq.top().v == d[i].v) { qq.pop(); continue; } else return 0; } } return 1; } int main(){ while(~scanf("%d", &n)) { for (int i = 0; i < n; ++i) { scanf("%d%d", &d[i].k, &d[i].v); } int fs = fstack(), fq = fqueue(), fpri = fpriqueue(); if (fs + fq + fpri > 1) printf("not sure\n"); else if (fs) printf("stack\n"); else if (fq) printf("queue\n"); else if (fpri) printf("priority queue\n"); else printf("impossible\n"); } return 0; }
用while(scanf("%d", &n))的话会超时 1.00s
用while(~scanf("%d", &n))的话只要 0.04
差距~~~~(>_<)~~~~
相关文章推荐
- uva 11995 I Can Guess the Data Structure!(数据结构:基础)
- UVA - 11995 I Can Guess the Data Structure! 猜猜数据结构(STL模拟)
- (UVA 11995) I Can Guess The Data Structure! --抽象数据结构判断
- (DS 《算法入门经典》)UVA 11995 I Can Guess the Data Structure!(判断是哪一种数据结构)
- I Can Guess the Data Structure! uva11995
- uva——11995——I Can Guess the Data Structure!
- UVA 11995 I Can Guess the Data Structure! (STL应用)
- UVA 11995 I Can Guess the Data Structure!<STL数据结构使用>
- (UVA - 11995)I Can Guess the Data Structure!
- UVA 11995 I Can Guess the Data Structure!【栈+队列+优先队列基本用法】
- UVA 11995 I Can Guess the Data Structure!
- UVA 11995 - I Can Guess the Data Structure! (基本数据结构)
- UVA - 11995 - I Can Guess the Data Structure!(栈、队列、优先队列)
- UVA 11995 I Can Guess the Data Structure!
- UVa 11995 I Can Guess the Data Structure(优先队列)
- UVA - 11995 I Can Guess the Data Structure!
- uva 11995——I Can Guess the Data Structure!
- UVa 11995 I Can Guess the Data Structure!
- 【模拟+数据结构】UVA 11995 I Can Guess the Data Structure!
- uva-11995 - I Can Guess the Data Structure!(栈,优先队列,队列,水题)