11 条题解
-
17
用数组模拟太复杂,直接上队列!(可惜队列没有size命令,只不过没关系,多定义一个sum,入队加一,出队减一)最后直接上代码
#include <bits/stdc++.h> using namespace std; int n, x, sum; string oper; queue<int> q; int main() { cin >> n; for (int i = 0; i < n; i++) { cin >> oper; if (oper == "IN") { cin >> x; sum++; q.push(x); } else if (oper == "OUT") { if (!(q.empty())) { sum--; q.pop(); } } else if (oper == "FRONT") { if (!(q.empty())) { cout << q.front() << endl; } } else { cout << sum << endl; } } return 0; }
-
5
#include <iostream> using namespace std; int n, x; string oper; int que[100005], head = 1, tail; int main() { cin >> n; for (int i = 0; i < n; i++) { cin >> oper; if (oper == "IN") { cin >> x; que[++tail] = x; } else if (oper == "OUT") { if (head <= tail) { head++; } } else if (oper == "FRONT") { if (head <= tail) { cout << que[head] << endl; } } else { cout << tail - head + 1 << endl; } } return 0; }
-
2
STL多好用,方便快捷:
#include <iostream> //三 #include <algorithm>//必 #include <cmath> //备 #include <queue> using namespace std; queue<int> q; int n,x; string doing; int main() { cin>>n; for (int i=1;i<=n;i++) { cin>>doing; if (doing=="IN") { cin>>x; q.push(x); } else if (doing=="OUT" && !(q.empty())) { q.pop(); } else if (doing=="FRONT" && !(q.empty())) { cout<<q.front()<<"\n"; } else if (doing=="SIZE") { cout<<q.size()<<"\n"; }//如果这里只写else连输出样例都过不了 } return 0; }
-
1
using namespace std; queue<int> q; int main() { int n; cin >> n; string s; int x; for (int i = 1;i <= n;i++) { cin >> s; if (s == "IN") { cin >> x; q.push(x); } else if (s == "OUT") { if (q.empty()) { continue; } q.pop(); } else if(s == "FRONT") { if (q.empty()) { continue; } cout << q.front() << endl; } else { cout << q.size() << endl; } } return 0; } //这确实有size指令啊!!! //搞不懂几位老兄在说什么 //已AC //这 //别忘点赞 //by hetao4582353 //不得不说,对于判断队列不为空时执行操作各位的表达方式确实不一样
`
-
1
#include <bits/stdc++.h> using namespace std; queue<int> q; int main() { int n , l , cnt = 0; string x; cin >> n; for( int i = 1 ; i <= n ; i++ ) { cin >> x; if(x == "IN") { cin >> l; q.push(l); cnt++; } if(x == "OUT" && !q.empty()) { q.pop(); cnt--; } if(x == "FRONT" && !q.empty()) { cout << q.front() << endl; } if(x == "SIZE") { cout << cnt << endl; } } return 0; }
-
1
课上了吧? A。没上 B。上了,不会 C。上了,会 如果你是B那就继续看
#include <bits/stdc++.h>//数组太难了,直接超纲用队列! using namespace std; int n, x, sum; string oper; queue<int> q; int main() { cin >> n; for (int i = 0; i < n; i++) { cin >> oper; if (oper == "IN") { cin >> x; sum++; q.push(x); } else if (oper == "OUT") { if (!(q.empty())) { sum--; q.pop(); } } else if (oper == "FRONT") { if (!(q.empty())) { cout << q.front() << endl; } } else { cout << sum << endl; } } return 0; }//by hetao11357143
所以,你会了吧?(课上的教了三个操作,我们不用数组实现,但思路一样,至于size指令直接搞一个sum记录就行了)
-
1
听了课就没问题 不知道大家在求长度的时候是用的什么方法呢 这里我单独定义了一个变量cnt用于记录长度 抄题解的要谨慎咯~
#include <bits/stdc++.h> using namespace std; int n, x, cnt; string oper; queue<int> q; int main() { cin >> n; for (int i = 0; i < n; i++) { cin >> oper; if (oper == "IN") { cin >> x; cnt++; q.push(x); } else if (oper == "OUT") { if (!(q.empty())) { cnt--; q.pop(); } } else if (oper == "FRONT") { if (!(q.empty())) { cout << q.front() << endl; } } else { cout << cnt << endl; } } return 0; }
-
0
简单队列
</span>#include <bits/stdc++.h> using namespace std; int n,x; string step; queue<int>q; int main(){ cin>>n; for (int i=1;i<=n;i++){ cin>>step; if (step=="IN"){ cin>>x; q.push(x); } else if(step=="OUT"&&q.size())q.pop(); else if(step=="FRONT"&&q.size())cout<<q.front()<<"\n"; else if(step=="SIZE")cout<<q.size()<<"\n"; } return 0; }
-
-1
#include <bits/stdc++.h> using namespace std; int n, x, sum; string oper; queue<int> q; int main() { cin >> n; for (int i = 0; i < n; i++) { cin >> oper; if (oper == "IN") { cin >> x; sum++; q.push(x); } else if (oper == "OUT") { if (!(q.empty())) { sum--; q.pop(); } } else if (oper == "FRONT") { if (!(q.empty())) { cout << q.front() << endl; } } else { cout << sum << endl; } } return 0; }
-
-1
#include <iostream> using namespace std; int n, x; string oper; int que[100005], head = 1, tail; int main() { cin >> n; for (int i = 0; i < n; i++) { cin >> oper; if (oper == "IN") { cin >> x; que[++tail] = x; } else if (oper == "OUT") { if (head <= tail) { head++; } } else if (oper == "FRONT") { if (head <= tail) { cout << que[head] << endl; } } else { cout << tail - head + 1 << endl; } } return 0; }
-
-1
本题和课上题目相比,只多了一个求队列元素个数的操作。由于队列范围是从队头标记开始,到队尾标记为止,因此输出
tail - head + 1
即可。代码
#include <iostream> using namespace std; int n, x; string oper; int que[100005], head = 1, tail; int main() { cin >> n; for (int i = 0; i < n; i++) { cin >> oper; if (oper == "IN") { cin >> x; que[++tail] = x; } else if (oper == "OUT") { if (head <= tail) head++; } else if (oper == "FRONT") { if (head <= tail) cout << que[head] << endl; } else { cout << tail - head + 1 << endl; } } return 0; }
- 1
信息
- ID
- 47
- 时间
- 1000ms
- 内存
- 256MiB
- 难度
- 4
- 标签
- (无)
- 递交数
- 1792
- 已通过
- 821
- 上传者