关于C++的cin&cout
  • 板块灌水区
  • 楼主while_true
  • 当前回复6
  • 已保存回复6
  • 发布时间2023/1/10 21:17
  • 上次更新2023/10/24 04:49:39
查看原帖
关于C++的cin&cout
661294
while_true楼主2023/1/10 21:17

我们发现,如果用其输出,其会在所有输入行为结束后输出。具体看代码:

请移步题目B3616【模板】队列

下面两份代码中,码一过不了,但是码二能过。原因如上所述。

CODE 1:

#include <bits/stdc++.h>

#define endl '\n'

using namespace std;

queue<int> Q;

int n;
int opt, x;

signed main() {
	
	ios::sync_with_stdio(false);
	cin.tie(0); cout.tie(0);
	
	cin >> n;
	
	while(n --) {
		cin >> opt;
		if(opt == 1) {
			cin >> x;
			Q.push(x);
		} else if(opt == 2) {
			if(Q.empty()) {
				puts("ERR_CANNOT_POP");
			} else {
				Q.pop();
			}
		} else if(opt == 3) {
			if(Q.empty()) {
				puts("ERR_CANNOT_QUERY");
			} else {
				cout << Q.front() << endl;
			}
		} else {
			cout << Q.size() << endl;
		}
	}
    
    return 0;
}

CODE 2:

#include <bits/stdc++.h>

#define endl '\n'

using namespace std;

queue<int> Q;

int n;
int opt, x;

signed main() {
	
	ios::sync_with_stdio(false);
	cin.tie(0); cout.tie(0);
	
	cin >> n;
	
	while(n --) {
		cin >> opt;
		if(opt == 1) {
			cin >> x;
			Q.push(x);
		} else if(opt == 2) {
			if(Q.empty()) {
				cout << "ERR_CANNOT_POP" << endl;
			} else {
				Q.pop();
			}
		} else if(opt == 3) {
			if(Q.empty()) {
				cout << "ERR_CANNOT_QUERY" << endl;
			} else {
				cout << Q.front() << endl;
			}
		} else {
			cout << Q.size() << endl;
		}
	}
    
    return 0;
}

这个原因很迷惑哈,有没有奆佬解答

2023/1/10 21:17
加载中...