1. 程式人生 > >c++中棧stack、queue的使用

c++中棧stack、queue的使用

包含標頭檔案 #include<stack>  和名稱空間 using namespace std

定義一個棧的語句:stack<int> stackOne,則:

入棧操作:int number = 3; stackOne.push(number);

出棧操作:stackOne.pop();

獲取棧頂元素:stackOne.top();

判斷棧是否為空:stackOne.empty();   //為空返回真

獲取棧頂元素個數:stackOne.size();

1.棧的輸入輸出簡單應用:

#include<iostream>
#include<stack>
#include<string>
using namespace std;

int main() {
	stack<char> stackOne;
	string str;
	int len;
	int t;
	cin >> t;
	
	while (t--) {
		cin >> str;
		len = str.length();
		for (int i = 0; i < len; i++) {
			stackOne.push(str[i]);
		}
		char c;
		for (int i = 0; i < len; i++) {
			c = stackOne.top();
			cout << c;
			stackOne.pop();
		}
		cout << endl;
	}
	return 0;
}

2.退格鍵的模擬

#include<iostream>
#include<stack>
#include<string>
using namespace std;

int main() {
	stack<char> stackOne;
	string str;
	int len;
	int t;
	cin >> t;
	
	while (t--) {
		cin >> str;
		len = str.length();
		int m = 0;
		for (int i = 0; i < len; i++) {
			if (m==0 && str[i] == '#') {
				continue;
			}
			if (str[i] != '#') {
				stackOne.push(str[i]);
				m++;
				continue;
			}
			if (m!= 0 && str[i] == '#') {
				stackOne.pop();
				continue;
			}
			
			
		}
		stack<char> stackTwo;
		for (int i = 0; i < m; i++) {
			stackTwo.push(stackOne.top());
			stackOne.pop();
		}
		char c;
		for (int i = 0; i < m; i++) {
			c = stackTwo.top();
			cout << c;
			stackTwo.pop();
		}
		cout << endl;
	}
	return 0;
}

只要前面有資料然後一碰到 # 就出問題,什麼情況?

報錯原因似乎是棧中沒有元素強行 pop ,但是沒什麼問題呀。

就是 stackOne.pop() 這條語句出了問題,好奇怪,程式可以判斷到#但就是不能進行出棧操作。

 

沒有 m--  !!!!!!

啊難受死了,出棧操作以後表示棧中元素數量的 m 沒有減一,真的太不嚴謹了。

 

終於通過:

#include<iostream>
#include<stack>
#include<string>
using namespace std;

int main() {
	stack<char> stackOne;
	string str;
	int len;
	int t;
	cin >> t;

	while (t--) {
		cin >> str;
		len = str.length();
		int m = 0;
		for (int i = 0; i < len; i++) {
			if (m == 0 && str[i] == '#') {
				continue;
			}
			if (str[i] != '#') {
				stackOne.push(str[i]);
				m++;
				continue;
			}
			if (m != 0 && str[i] == '#') {
					stackOne.pop();
					m--;
				continue;
			}
		}
		if (m == 0) {
			cout << "NULL" << endl;
			continue;
		}
		stack<char> stackTwo;
		for (int i = 0; i < m; i++) {
			stackTwo.push(stackOne.top());
			stackOne.pop();
		}
		char c;
		for (int i = 0; i < m; i++) {
			c = stackTwo.top();
			cout << c;
			stackTwo.pop();
		}
		cout << endl;
	}
	return 0;
}

3.括號的匹配

為什麼總是 WA?

#include<iostream>
#include<stack>
#include<string>
using namespace std;

int main() {
	stack<char> stackOne;
	string str;
	int len;
	int t;
	cin >> t;

	while (t--) {
		cin >> str;
		len = str.length();
		int m = 0;
		char top;
		for (int i = 0; i < len; i++) {
			if (str[i] == '{' || str[i] == '[' || str[i] == '(') {
				stackOne.push(str[i]);
				m++;
			}
			if (str[i] == ')') {
				if(m==0) {
					cout << "arror" << endl;
					return 0;
				}
				else {
					top = stackOne.top();
					if (top == '(') {
						stackOne.pop();
						m--;
					}
					else {
						cout << "arror" << endl;
						return 0;
					}
				}
			}
			if (str[i] == ']') {
				if (m == 0) {
					cout << "arror" << endl;
					return 0;
				}
				else {
					top = stackOne.top();
					if (top == '[') {
						stackOne.pop();
						m--;
					}
					else {
						cout << "arror" << endl;
						return 0;
					}
				}
			}
			if (str[i] == '}') {
				if (m == 0) {
					cout << "arror" << endl;
					return 0;
				}
				else {
					top = stackOne.top();
					if (top == '{') {
						stackOne.pop();
						m--;
					}
					else {
						cout << "arror" << endl;
						return 0;
					}
				}
			}
		}
		if (m == 0)
			cout << "ok" << endl;
		else
			cout << "arror" << endl;
	}
	return 0;
}

樣例沒問題,感覺自己考慮的也還算周全呀

 

我希望每一次有問題的時候,直接跳到下一個 while 中,而不是繼續在 for 裡面。

#include<iostream>
#include<stack>
#include<string>
using namespace std;

int main() {
	stack<char> stackOne;
	string str;
	int len;
	int t;
	cin >> t;

	while (t--) {
		cin >> str;
		len = str.length();
		int m = 0;
		char top;
		int k = 0;
		for (int i = 0; i < len; i++) {

			if (str[i] == '{' || str[i] == '[' || str[i] == '(') {
				stackOne.push(str[i]);
				m++;
			}
			if (str[i] == ')') {
				if (m == 0) {
					k = 1;
					break;
				}
				else {
					top = stackOne.top();
					if (top == '(') {
						stackOne.pop();
						m--;
					}
					else {
						k = 1;
						break;
					}
				}
			}
			if (str[i] == ']') {
				if (m == 0) {
					k = 1;
					break;
				}
				else {
					top = stackOne.top();
					if (top == '[') {
						stackOne.pop();
						m--;
					}
					else {
						k = 1;
						break;
					}
				}
			}
			if (str[i] == '}') {
				if (m == 0) {
					k = 1;
					break;

				}
				else {
					top = stackOne.top();
					if (top == '{') {
						stackOne.pop();
						m--;
					}
					else {
						k = 1;
						break;
					}
				}
			}
		}
		if (k == 0&& m == 0) {
				cout << "ok" << endl;
		}
		else {
			cout << "arror" << endl;
		}
	}
	return 0;
}

修正了很多,但是依然WA ,

#include<iostream>
#include<stack>
#include<string>
using namespace std;

int main() {
	stack<char> stackOne;
	string str;
	int len;
	int t;
	cin >> t;

	while (t--) {
		cin >> str;
		len = str.length();
		int m = 0;
		char top;
		int k = 0;
		for (int i = 0; i < len; i++) {

			if (str[i] == '{' || str[i] == '[' || str[i] == '(') {
				stackOne.push(str[i]);
				m++;
			}
			if (str[i] == ')') {
				if (m == 0) {
					k = 1;
					break;
				}
				else {
					top = stackOne.top();
					if (top == '(') {
						stackOne.pop();
						m--;
					}
					else {
						k = 1;
						break;
					}
				}
			}
			if (str[i] == ']') {
				if (m == 0) {
					k = 1;
					break;
				}
				else {
					top = stackOne.top();
					if (top == '[') {
						stackOne.pop();
						m--;
					}
					else {
						k = 1;
						break;
					}
				}
			}
			if (str[i] == '}') {
				if (m == 0) {
					k = 1;
					break;

				}
				else {
					top = stackOne.top();
					if (top == '{') {
						stackOne.pop();
						m--;
					}
					else {
						k = 1;
						break;
					}
				}
			}
		}
		if (k == 0&& m == 0) {
				cout << "ok" << endl;
		}
		else {
			cout << "arror" << endl;
		}
	}
	return 0;
}
#include<iostream>
#include<stack>
#include<string>
using namespace std;

int main() {
	stack<char> stackOne;
	string str;
	int len;
	int t;
	cin >> t;

	while (t--) {
		cin >> str;
		len = str.length();
		int m = 0;
		char top;
		int k = 0;
		for (int i = 0; i < len; i++) {

			if (str[i] == '{' || str[i] == '[' || str[i] == '(') {
				stackOne.push(str[i]);
				m++;
			}
			if (str[i] == ')') {
				if (m == 0) {
					k = 1;
					break;
				}
				else {
					top = stackOne.top();
					if (top == '(') {
						stackOne.pop();
						m--;
					}
					else {
						k = 1;
						break;
					}
				}
			}
			if (str[i] == ']') {
				if (m == 0) {
					k = 1;
					break;
				}
				else {
					top = stackOne.top();
					if (top == '[') {
						stackOne.pop();
						m--;
					}
					else {
						k = 1;
						break;
					}
				}
			}
			if (str[i] == '}') {
				if (m == 0) {
					k = 1;
					break;

				}
				else {
					top = stackOne.top();
					if (top == '{') {
						stackOne.pop();
						m--;
					}
					else {
						k = 1;
						break;
					}
				}
			}
		}
		if (k == 0&& m == 0) {
				cout << "ok" << endl;
		}
		else {
			cout << "arror" << endl;
		}
	}
	return 0;
}

4.數制轉換

遇到的第一個問題,如何知道是幾位小數?我希望吧小數部分變成整數再處理。

......沒看懂題意吧你

如何知道佇列或棧的長度?定義一個int 變數push 一次加一不就行了

#include<iostream>
#include<stack>
#include<queue>
#include<cmath>
#include <iomanip>
using namespace std;

int main() {
	stack<int> sone;
	queue<int> qone;
	int t;
	cin >> t;
	while (t--) {
		double n;
		int k;
		cin >> n >> k;
		int inte;
		double deci;
		inte = n / 1;
		deci = n - inte;//為什麼deci=n%1會報錯?double n 表示式必須具有整數和未區分範圍的列舉型別?
		int snum = 0, qnum = 0;
		int p, q;
		if (inte != 0) {
			int con;
			p = inte / k;
			q = inte % k;
			con = p;
			sone.push(q);
			snum++;
			while (p) {
				p = con / k;
				q = con % k;
				con = p;
				sone.push(q);
				snum++;
			}
		}
		double s, t;
		if (deci != 0) {
			double m;
			s = deci * k;
			t = s / 1;
			m = s - t;
			qone.push(t);
			qnum++;
			while (m) {
				s = deci * k;
				t = s / 1;
				m = s - t;
				qone.push(t);
				qnum++;
			}
		}
		double ans = 0.0;
		for (int i = 0; i < snum; i++) {
			ans += sone.top()*pow(10, i);
			sone.pop();
		}
		for (int i = 0; i < qnum; i++) {
			ans += qone.front()*pow(0.1, i+1);
			sone.pop();
		}
		cout << fixed << setprecision(3) << ans << endl;

	}
	return 0;
}

有問題,自己的想法依然是十進位制,要是樣例不是那麼有特點,可能一直都發現不了錯誤,但是2進位制不應該有錯呀,似乎又出現了訪問錯誤記憶體的情況。

#include<iostream>
#include<stack>
#include<queue>
#include<cmath>
#include <iomanip>
using namespace std;

int main() {
	stack<char> sone;
	queue<char> qone;
	int t;
	cin >> t;
	while (t--) {
		double n;
		int k;
		cin >> n >> k;
		int inte;
		double deci;
		inte = n / 1;
		deci = n - inte;//為什麼deci=n%1會報錯?double n 表示式必須具有整數和未區分範圍的列舉型別?
		int snum = 0, qnum = 0;
		char p, q;
		if (inte != 0) {
			char con;
			p = inte / k;
			q = inte % k;
			con = p;
			switch (q) {
			case 10:p = 'A'; break;
			case 11:p = 'B'; break;
			case 12:p = 'C'; break;
			case 13:p = 'D'; break;
			case 14:p = 'E'; break;
			case 15:p = 'F'; break;
			default:;
			}
			sone.push(q);
			snum++;
			while (p) {
				p = con / k;
				q = con % k;
				con = p;
				switch (q) {
				case 10:p = 'A'; break;
				case 11:p = 'B'; break;
				case 12:p = 'C'; break;
				case 13:p = 'D'; break;
				case 14:p = 'E'; break;
				case 15:p = 'F'; break;
				default:;
				}
				sone.push(q);
				snum++;
			}
		}
		char s, t;
		if (deci != 0) {
			double m;
			s = deci * k;
			t = s / 1;
			m = s - t;
			switch (t) {
			case 10:t = 'A'; break;
			case 11:t = 'B'; break;
			case 12:t = 'C'; break;
			case 13:t = 'D'; break;
			case 14:t = 'E'; break;
			case 15:t = 'F'; break;
			default:;
			}
			qone.push(t);
			qnum++;
			while (m) {
				s = deci * k;
				t = s / 1;
				m = s - t;
				switch (t) {
				case 10:t = 'A'; break;
				case 11:t = 'B'; break;
				case 12:t = 'C'; break;
				case 13:t = 'D'; break;
				case 14:t = 'E'; break;
				case 15:t = 'F'; break;
				default:;
				}
				qone.push(t);
				qnum++;
			}
		}
		double ans = 0.0;
		for (int i = 0; i < snum; i++) {
			ans += sone.top()*pow(10, i);
			sone.pop();
		}
		for (int i = 0; i < qnum; i++) {
			ans += qone.front()*pow(0.1, i+1);
			sone.pop();
		}
		cout << fixed << setprecision(3) << ans << endl;
	}
	return 0;
}

對自己寫的這串程式碼莫名其妙,char 和 double 還有 int 之間的計算是什麼樣的? 如何把棧中輸出的一個一個單獨的字元組合成double 型數字?

似乎發現了c++直接定義某進位制資料的方式,不知道是否能起到幫助

目前我的困惑是老師規定的三位輸出資料的情況,並且還要求轉換成資料儲存下來,而不是僅僅的輸出,要是僅僅簡單的輸出的haul那就相當簡單了。我其實可以先都用int 儲存到棧中,出棧輸出時再輸出成字元型。

 

我先按簡單的處理一下看看能不能AC。

#include<iostream>
#include<stack>
#include<queue>
#include<cmath>
using namespace std;

int main() {
	stack<int> sone;
	queue<int> qone;
	int t;
	cin >> t;
	while (t--) {
		double n;
		int k;
		cin >> n >> k;
		int inte;
		double deci;
		inte = n / 1;
		deci = n - inte;
		int snum = 0, qnum = 0;
		int p, q;
		if (inte != 0) {
			int con;
			p = inte / k;
			q = inte % k;
			con = p;
			sone.push(q);
			snum++;
			while (p) {
				p = con / k;
				q = con % k;
				con = p;
				sone.push(q);
				snum++;
			}
		}
		double s;
		int t;
		if (deci != 0.0) {
			double m;
			s = deci * k;
			t = s / 1;
			m = s - t;
			qone.push(t);
			qnum++;
			while (m) {
				s = m * k;
				t = s / 1;
				m = s - t;
				qone.push(t);
				qnum++;
			}
		}
		int h;
		for (int i = 0; i < snum; i++) {
			h = sone.top();
			switch (h) {
			case 10:cout << 'A'; break;
			case 11:cout << 'B'; break;
			case 12:cout << 'C'; break;
			case 13:cout << 'D'; break;
			case 14:cout << 'E'; break;
			case 15:cout << 'F'; break;
			default:cout << h;
			}
			sone.pop();
		}
		cout << ".";
		if (qnum >= 3) {
			for (int i = 0; i < 3; i++) {
				h = qone.front();
				switch (h) {
				case 10:cout << 'A'; break;
				case 11:cout << 'B'; break;
				case 12:cout << 'C'; break;
				case 13:cout << 'D'; break;
				case 14:cout << 'E'; break;
				case 15:cout << 'F'; break;
				default:cout << h;
				}
				sone.pop();
			}
		}
		else {
			for (int i = 0; i < qnum; i++) {
				h = qone.front();
				switch (h) {
				case 10:cout << 'A'; break;
				case 11:cout << 'B'; break;
				case 12:cout << 'C'; break;
				case 13:cout << 'D'; break;
				case 14:cout << 'E'; break;
				case 15:cout << 'F'; break;
				default:cout << h;
				}
				sone.pop();
			}
			for (int i = 0; i < 3 - qnum; i++) {
				cout << '0';
			}
		}
		cout << endl;
	}
	return 0;
}

整數部分輸出沒有問題,但是小數部分有問題,應該是記憶體訪問出錯的問題。

剛剛是一個地方把 qone 打成了 sone ,自然會出錯

現在成功啦!果然想複雜了不大好

#include<iostream>
#include<stack>
#include<queue>
#include<cmath>
using namespace std;

int main() {
	stack<int> sone;
	queue<int> qone;
	int t;
	cin >> t;
	while (t--) {
		double n;
		int k;
		cin >> n >> k;
		int inte;
		double deci;
		inte = n / 1;
		deci = n - inte;
		int snum = 0, qnum = 0;
		int p, q;
		if (inte != 0) {
			int con;
			p = inte / k;
			q = inte % k;
			con = p;
			sone.push(q);
			snum++;
			while (p) {
				p = con / k;
				q = con % k;
				con = p;
				sone.push(q);
				snum++;
			}
		}
		double s;
		int t;
		if (deci != 0.0) {
			double m;
			s = deci * k;
			t = s / 1;
			m = s - t;
			qone.push(t);
			qnum++;
			while (m) {
				s = m * k;
				t = s / 1;
				m = s - t;
				qone.push(t);
				qnum++;
			}
		}
		int h;
		if (snum != 0) {
			for (int i = 0; i < snum; i++) {
				h = sone.top();
				switch (h) {
				case 10:cout << 'A'; break;
				case 11:cout << 'B'; break;
				case 12:cout << 'C'; break;
				case 13:cout << 'D'; break;
				case 14:cout << 'E'; break;
				case 15:cout << 'F'; break;
				default:cout << h;
				}
				sone.pop();
			}
		}
		else 
			cout << "0";

		cout << ".";
		if (qnum >= 3) {
			for (int i = 0; i < 3; i++) {
				h = qone.front();
				switch (h) {
				case 10:cout << 'A'; break;
				case 11:cout << 'B'; break;
				case 12:cout << 'C'; break;
				case 13:cout << 'D'; break;
				case 14:cout << 'E'; break;
				case 15:cout << 'F'; break;
				default:cout << h;
				}
				qone.pop();
			}
		}
		else {
			for (int i = 0; i < qnum; i++) {
				h = qone.front();
				switch (h) {
				case 10:cout << 'A'; break;
				case 11:cout << 'B'; break;
				case 12:cout << 'C'; break;
				case 13:cout << 'D'; break;
				case 14:cout << 'E'; break;
				case 15:cout << 'F'; break;
				default:cout << h;
				}
				qone.pop();
			}
			for (int i = 0; i < 3 - qnum; i++) {
				cout << '0';
			}
		}
		cout << endl;
	}
	return 0;
}

但是老師的提示沒有用到呀,唉,感覺不大對勁。不過一次提交就AC的感覺第一次體會到。

一會問問群上大佬吧

 

5.組佇列

大體框架已經準備好,現在面臨的問題:1.佇列的查詢2.如何判斷是哪一組的,並插入到組的末尾

#include<iostream>
#include<queue>
#include<string>
using namespace std;

int main() {
	queue<int> qone;
	queue<int> qtwo;
	int n, m, p, num1 = 0, num2 = 0;
	cin >> n;
	while (n--) {
		cin >> m;
		for (int i = 0; i < m; i++) {
			cin >> p;
			qone.push(p);
			num1++;
		}
	}
	string com1 = "ENQUEUE";
	string com2 = "DNQUEUE";
	string com3 = "STOP";
	string com;
	int data;
	while (cin >> com) {
		if (com == "ENQUEUE") {
			cin >> data;

		}
		if (com == "DNQUEUE") {
			qtwo.push(qone.front());
			num2++;
			qone.pop();
		}
		if (com == "STOP") {
			break;
		}
	}
	for (int i = 0; i < num2; i++) {
		cout << qtwo.front() << " ";
		qtwo.pop();
	}
	return 0;
}

佇列中沒有查詢這種方法,可以這樣,一個一個出隊再入隊,當找到相同的元素時可以記下是第幾個,由此判斷屬於第幾組,之後在出隊到該組末尾時插入,穩的!

組的數量是動態的,可以建立一個數組,有幾組元素就在前幾個位置中放入組的長度。

#include<iostream>
#include<queue>
#include<string>
const int MAX = 1000;
using namespace std;

int main() {
	queue<int> qone;
	queue<int> qtwo;
	int n, m, p, num1 = 0, num2 = 0;
	int group[MAX];
	memset(group, 0, MAX);
	int groupLine;
	cin >> n;
	groupLine = n;
	while (n--) {
		cin >> m;
		group[groupLine-n] = m;
		for (int i = 0; i < m; i++) {
			cin >> p;
			qone.push(p);
		}
	}
	string com1 = "ENQUEUE";
	string com2 = "DNQUEUE";
	string com3 = "STOP";
	string com;
	int data;
	while (cin >> com) {
		if (com == "ENQUEUE") {
			cin >> data;

		}
		if (com == "DNQUEUE") {
			qtwo.push(qone.front());
			qone.pop();
		}
		if (com == "STOP") {
			break;
		}
	}
	for (int i = 0; i < qtwo.size(); i++) {
		cout << qtwo.front() << " ";
		qtwo.pop();
	}
	return 0;
}

奇怪的情況,沒有任何輸出和報錯

#include<iostream>
#include<queue>
#include<string>
const int MAX = 1000;
using namespace std;

int main() {
	queue<int> qone;
	queue<int> qtwo;
	int n, m, p, num1 = 0, num2 = 0;
	int group[MAX];
	memset(group, 0, MAX);
	int groupLine;
	cin >> n;
	groupLine = n;
	while (n--) {
		cin >> m;
		group[groupLine-n] = m;
		for (int i = 0; i < m; i++) {
			cin >> p;
			qone.push(p);
		}
	}
	string com1 = "ENQUEUE";
	string com2 = "DNQUEUE";
	string com3 = "STOP";
	string com;
	int data;
	while (cin >> com) {
		if (com == "ENQUEUE") {
			cin >> data;
			int patrol, patrol2 = 0, sentinel = 0;
			for (int i = 1; i <= qone.size(); i++) {
				patrol = qone.front();
				if (i == sentinel) {
					qone.push(data);
					patrol2 = 1;
				}
				else {
					if (data == patrol) {
						int groudNum = group[1];
						for (int j = 1; j <= groupLine; j++) {
							if (i < groudNum) {
								sentinel = groudNum;
								break;
							}
							else {
								groudNum += group[j + 1];
							}
						}

					}
				}
				qone.pop();
				qone.push(patrol);
			}
			if (patrol2 != 1) {
				qone.push(data);
			}
		}
		if (com == "DNQUEUE") {
			qtwo.push(qone.front());
			qone.pop();
		}
		if (com == "STOP") {
			break;
		}
	}
	for (int i = 0; i < qtwo.size(); i++) {
		cout << qtwo.front() << " ";
		qtwo.pop();
	}
	return 0;
}

修改了一下,目前是插入資料的部分有問題,奇奇怪怪

#include<iostream>
#include<queue>
#include<string>
const int MAX = 1000;
using namespace std;

int main() {
	queue<int> qone;
	queue<int> qtwo;
	int n, m, p, num1 = 0, num2 = 0;
	int group[MAX];
	memset(group, 0, MAX);
	int groupLine;
	cin >> n;
	groupLine = n;
	while (n--) {
		cin >> m;
		group[groupLine-n] = m;
		for (int i = 0; i < m; i++) {
			cin >> p;
			qone.push(p);
		}
	}
	string com;
	int data;
	while (cin >> com) {
		if (com == "ENQUEUE") {
			cin >> data;
			int patrol, patrol2 = 0, sentinel = 0;
			for (int i = 1; i <= qone.size(); i++) {
				patrol = qone.front();
				if (i == sentinel) {
					qone.push(data);
					patrol2 = 1;
				}
				else {
					if (data == patrol) {
						int groudNum = group[1];
						for (int j = 1; j <= groupLine; j++) {
							if (i < groudNum) {
								sentinel = groudNum;
								break;
							}
							else {
								groudNum += group[j + 1];
							}
						}

					}
				}
				qone.pop();
				qone.push(patrol);
			}
			if (patrol2 != 1) {
				qone.push(data);
			}
		}
		if (com == "DNQUEUE") {
			qtwo.push(qone.front());
			qone.pop();
		}
		if (com == "STOP") {
			break;
		}
	}
	int ansnum = qtwo.size();
	for (int i = 0; i < ansnum; i++) {
		cout << qtwo.front() << " ";
		qtwo.pop();
	}
	return 0;
}

我輸入的資料全都塞在了隊尾,查詢和插入都是失敗的

#include<iostream>
#include<queue>
#include<string>
const int MAX = 1000;
using namespace std;

int main() {
	queue<int> qone;
	queue<int> qtwo;
	int n, m, p, num1 = 0, num2 = 0;
	int group[MAX];
	memset(group, 0, MAX);
	int groupLine;
	cin >> n;
	groupLine = n;
	while (n--) {
		cin >> m;
		group[groupLine-n] = m;
		for (int i = 0; i < m; i++) {
			cin >> p;
			qone.push(p);
		}
	}
	string com;
	int data;
	while (cin >> com) {
		if (com == "ENQUEUE") {
			cin >> data;
			int patrol, patrol2 = 0, sentinel = -1;
			for (int i = 1; i <= qone.size(); i++) {
				patrol = qone.front();
				if (i-1 == sentinel) {
					qone.push(data);
					patrol2 = 1;
					break;
				}
				else {
					if (data == patrol) {
						int groudNum = group[1];
						for (int j = 1; j <= groupLine; j++) {
							if (i < groudNum) {
								sentinel = groudNum;
								break;
							}
							else {
								groudNum += group[j + 1];
							}
						}

					}
				}
				qone.pop();
				qone.push(patrol);
			}
			if (patrol2 != 1) {
				qone.push(data);
			}
		}
		if (com == "DNQUEUE") {
			qtwo.push(qone.front());
			qone.pop();
		}
		if (com == "STOP") {
			break;
		}
	}
	int ansnum = qtwo.size();
	for (int i = 0; i < ansnum; i++) {
		cout << qtwo.front() << " ";
		qtwo.pop();
	}
	return 0;
}

好奇怪,自己嘗試很符合題意,為什麼樣例就過不了?跟三有關?

前面的插入對後面的產生了影響。插入後變多了,此時i-1 與的判斷就會出錯。

最後一組的末尾和佇列末尾可以等同對待嗎?

先認為相同,看一下

 

依然不對,要自閉了,苦

#include<iostream>
#include<queue>
#include<string>
const int MAX = 1000;
using namespace std;

int main() {
	queue<int> qone;
	queue<int> qtwo;
	int n, m, p, num1 = 0, num2 = 0;
	int group[MAX];
	memset(group, 0, MAX);
	int groupLine;
	cin >> n;
	groupLine = n;
	while (n--) {
		cin >> m;
		group[groupLine-n] = m;
		for (int i = 0; i < m; i++) {
			cin >> p;
			qone.push(p);
		}
	}
	string com;
	int data;
	while (cin >> com) {
		if (com == "ENQUEUE") {
			cin >> data;
			int patrol, patrol2 = 0, sentinel = -1;
			for (int i = 1; i <= qone.size(); i++) {
				patrol = qone.front();
				if (i-1 == sentinel) {
					qone.push(data);
					patrol2 = 1;

					break;
				}
				else {
					if (data == patrol) {
						int groudNum = group[1];
						for (int j = 1; j <= groupLine; j++) {
							if (i < groudNum) {
								sentinel = groudNum;
								group[j] +=1;
								break;
							}
							else {
								groudNum += group[j + 1];
							}
						}

					}
				}
				qone.pop();
				qone.push(patrol);
			}
			if (patrol2 != 1) {
				qone.push(data);
				group[n]+=1;//baocuo
			}
		}
		if (com == "DNQUEUE") {
			qtwo.push(qone.front());
			qone.pop();
		}
		if (com == "STOP") {
			break;
		}
	}
	int ansnum = qtwo.size();
	for (int i = 0; i < ansnum; i++) {
		cout << qtwo.front() << " ";
		qtwo.pop();
	}
	return 0;
}

輸出簡直亂七八糟,還陣列訪問越界報錯

明天再說吧,做不下去了

#include<iostream>
#include<queue>
#include<string>
const int MAX = 1000;
using namespace std;

int main() {
	queue<int> qone;
	queue<int> qtwo;
	int n, m, p, num1 = 0, num2 = 0;
	int group[MAX];
	memset(group, 0, MAX);
	int groupLine;
	cin >> n;
	groupLine = n;
	while (n--) {
		cin >> m;
		group[groupLine-n] = m;
		for (int i = 0; i < m; i++) {
			cin >> p;
			qone.push(p);
		}
	}
	string com;
	int data;
	while (cin >> com) {
		if (com == "ENQUEUE") {
			cin >> data;
			int patrol, patrol2 = 0, sentinel = -1;
			for (int i = 1; i <= qone.size(); i++) {
				patrol = qone.front();
				if (i-1 == sentinel) {
					qone.push(data);
					patrol2 = 1;//這裡沒有break!依然要傳遞!
				}
				else {
					if (data == patrol) {
						int groudNum = group[1];
						for (int j = 1; j <= groupLine; j++) {
							if (i < groudNum) {
								sentinel = groudNum;
								group[j] +=1;
								break;
							}
							else {
								groudNum += group[j + 1];
							}
						}

					}
				}
				qone.pop();
				qone.push(patrol);
			}
			if (patrol2 != 1) {
				qone.push(data);
				group[n]+=1;//baocuo
			}
		}
		if (com == "DNQUEUE") {
			qtwo.push(qone.front());
			qone.pop();
		}
		if (com == "STOP") {
			break;
		}
	}
	int ansnum = qtwo.size();
	for (int i = 0; i < ansnum; i++) {
		cout << qtwo.front() << " ";
		qtwo.pop();
	}
	return 0;
}

現在的情況是,輸出是正確的,但是最後會棧空間超出。但是認為佇列末尾應該算在下一組上,而不是最後一組的末尾

現在,將本在第二組插入的元素查到了第一組

#include<iostream>
#include<queue>
#include<string>
const int MAX = 1000;
using namespace std;

int main() {
	queue<int> qone;
	queue<int> qtwo;
	int n, m, p;
	int group[MAX];
	memset(group, 0, MAX);
	int groupLine;
	cin >> n;
	groupLine = n;
	while (n--) {
		cin >> m;
		group[groupLine-n] = m;
		for (int i = 0; i < m; i++) {
			cin >> p;
			qone.push(p);
		}
	}
	string com;
	int data;
	while (cin >> com) {
		if (com == "ENQUEUE") {
			cin >> data;
			int patrol, patrol2 = 0, sentinel = -1;
			int qosize = qone.size();
			for (int i = 1; i <= qosize; i++) {
				patrol = qone.front();
				if (i-1 == sentinel) {
					qone.push(data);
					patrol2 = 1;
				}
				else {
					if (data == patrol) {
						int groudNum = group[1];
						for (int j = 1; j <= groupLine; j++) {
							if (i < groudNum) {
								sentinel = groudNum;
								group[j] +=1;
								break;
							}
							else {
								groudNum += group[j + 1];
							}
						}

					}
				}
				qone.pop();
				qone.push(patrol);
			}
			if (patrol2 != 1) {
				qone.push(data);
				n++;
				groupLine++;
				group[n+1]++;
			}
		}
		if (com == "DNQUEUE") {
			qtwo.push(qone.front());
			qone.pop();
		}
		if (com == "STOP") {
			break;
		}
	}
	int qtsize = qtwo.size();
	for (int i = 0; i < qtsize; i++) {
		cout << qtwo.front();
		qtwo.pop();
		if (i < qtsize-1)
			cout << " ";
	}
	return 0;
}

進入迴圈前用了 n--  !所以之後的n已經是0了,再用 group[n+1]++ 自然會出錯,但是這對本該在第二組的元素跑到第一組沒影響呀。樣例還是什麼輸出都沒有直接結束,o(╥﹏╥)o

你丫的又讀錯題了搞了這麼久不心痛嗎......DEQUEUE 看成了 DNQUEUE ,有輸出才怪好嗎。自己的編譯已經沒錯了,但就是出現“編譯錯誤”這個問題。

#include<iostream>
#include<queue>
#include<string>
#include<cstdlib>
const int MAX = 1000;
using namespace std;

int main() {
	queue<int> qone;
	queue<int> qtwo;
	int n, m, p;
	int group[MAX];
	memset(group, 0, MAX);
	int groupLine;
	cin >> n;
	groupLine = n;
	while (n--) {
		cin >> m;
		group[groupLine-n] = m;
		for (int i = 0; i < m; i++) {
			cin >> p;
			qone.push(p);
		}
	}
	string com;
	int data;
	while (cin >> com) {
		if (com == "ENQUEUE") {
			cin >> data;
			int patrol, patrol2 = 0, sentinel = -1;
			int qosize = qone.size();
			for (int i = 1; i <= qosize; i++) {
				patrol = qone.front();
				if (i-1 == sentinel) {
					qone.push(data);
					patrol2 = 1;
				}
				else {
					if (data == patrol) {
						int groudNum = group[1];
						for (int j = 1; j <= groupLine; j++) {
							if (i < groudNum) {
								sentinel = groudNum;
								group[j] +=1;
								break;
							}
							else {
								groudNum += group[j + 1];
							}
						}

					}
				}
				qone.pop();
				qone.push(patrol);
			}
			if (patrol2 != 1) {
				qone.push(data);
				groupLine++;
				group[groupLine+1]++;
			}
		}
		if (com == "DEQUEUE") {
			qtwo.push(qone.front());
			qone.pop();
		}
		if (com == "STOP") {
			break;
		}
	}
	int qtsize = qtwo.size();
	for (int i = 0; i < qtsize; i++) {
		cout << qtwo.front();
		qtwo.pop();
		if (i < qtsize-1)
			cout << " ";
	}
	return 0;
}

mement() 是在string.h 裡面!string.h 和string 不是同一個標頭檔案!

oj有毒吧,還說什麼 cstdlib 。

修改後,答案錯誤50%,已經接近成功了,加油!

#include<iostream>
#include<queue>
#include<string>
#include<string.h>
const int MAX = 1000;
using namespace std;

int main() {
	queue<int> qone;
	queue<int> qtwo;
	int n, m, p;
	int group[MAX];
	memset(group, 0, MAX);
	int groupLine;
	cin >> n;
	groupLine = n;
	while (n--) {
		cin >> m;
		group[groupLine-n] = m;
		for (int i = 0; i < m; i++) {
			cin >> p;
			qone.push(p);
		}
	}
	string com;
	int data;
	while (cin >> com) {
		if (com == "ENQUEUE") {
			cin >> data;
			int patrol, patrol2 = 0, sentinel = -1;
			int qosize = qone.size();
			for (int i = 1; i <= qosize; i++) {
				patrol = qone.front();
				if (i-1 == sentinel) {
					qone.push(data);
					patrol2 = 1;
				}
				else {
					if (data == patrol) {
						int groudNum = group[1];
						for (int j = 1; j <= groupLine; j++) {
							if (i < groudNum) {
								sentinel = groudNum;
								group[j] +=1;
								break;
							}
							else {
								groudNum += group[j + 1];
							}
						}

					}
				}
				qone.pop();
				qone.push(patrol);
			}
			if (patrol2 != 1) {
				qone.push(data);
				groupLine++;
				group[groupLine+1]++;
			}
		}
		if (com == "DEQUEUE") {
			qtwo.push(qone.front());
			qone.pop();
		}
		if (com == "STOP") {
			break;
		}
	}
	int qtsize = qtwo.size();
	for (int i = 0; i < qtsize; i++) {
		cout << qtwo.front();
		qtwo.pop();
		if (i < qtsize-1)
			cout << " ";
	}
	return 0;
}

自己的理解有問題,改了以後第一次嘗試失敗。什麼時候才可以一遍打完就ok呢,不說提交以後AC, 起碼本地編譯沒錯也行呀

#include<iostream>
#include<string>
#include<queue>
#include<string.h>
using namespace std;

int main() {
	int n;
	cin >> n;
	queue<int> group[10];
	int groupNum[10];
	memset(groupNum, 0, 10);
	for (int i = 0; i < n; i++) {
		int m;
		cin >> m;
		int num;
		for (int j = 0; j < m; j++) {
			cin >> num;
			group[i].push(num);
		}
	}
	string com;
	queue<int> qone;
	queue<int> qtwo;
	int first = -1;
	int patrol;
	while (cin >> com) {
		if (com == "ENQUEUE") {
			int data;
			cin >> data;
			for (int i = 0; i < n; i++) {
				for (int j = 0; j < group[i].size(); j++) {
					patrol = group[i].front();
					if (data == patrol) {
						first = i;
						groupNum[first]++;
					}
					group[i].pop();
					group[i].push(patrol);
				}
			}
			if (first == -1) {
				qone.push(data);
			}
			else {
				if (qone.empty()) {
					qone.push(data);
				}
				else {
					int sentinel = 0;
					for (int i = 0; i < qone.size(); i++) {
						qone.push(qone.front());
						if (sentinel == groupNum[first]) {
							qone.push(data);
						}
						patrol = qone.front();
						for (int j = 0; j < group[first].size(); j++) {
							if (patrol == group[first].front())
								sentinel++;
							group[first].push(group[first].front());
							group[first].pop();
						}
						qone.pop();
					}
				}
			}

		}
		if (com == "DEQUEUE") {
			qtwo.push(qone.front());
			qone.pop();
		}
		if (com == "STOP") {
			break;
		}
	}
	int qtsize = qtwo.size();
	for (int i = 0; i < qtsize; i++) {
		cout << qtwo.front();
		qtwo.pop();
		if (i < qtsize - 1)
			cout << " ";

	}
	return 0;
}

現在的問題是,同組的元素進不去,其他組的第一個元素以及非該組元素都是可以進的

#include<iostream>
#include<string>
#include<queue>
#include<string.h>
using namespace std;

int main() {
	int n;
	cin >> n;
	queue<int> group[10];
	int groupNum[10];
	memset(groupNum, 0, 10);
	for (int i = 0; i < n; i++) {
		int m;
		cin >> m;
		int num;
		for (int j = 0; j < m; j++) {
			cin >> num;
			group[i].push(num);
		}
	}
	string com;
	queue<int> qone;
	queue<int> qtwo;
	int first ;
	while (cin >> com) {
		if (com == "E") {
			int patrol;
			int data;
			first = -1;
			cin >> data;
			for (int i = 0; i < n; i++) {
				for (int j = 0; j < group[i].size(); j++) {
					patrol = group[i].front();
					if (data == patrol) {
						first = i;
						groupNum[first]++;
					}
					group[i].pop();
					group[i].push(patrol);
				}
			}
			if (first == -1) {
				qone.push(data);
			}
			else {
				if (qone.empty()) {
					qone.push(data);
				}
				else {
					int sentinel = 0;
					int qsize3 = qone.size();
					for (int i = 0; i < qsize3; i++) {
						if (groupNum[first] - 1 == 0) {
							qone.push(data);
							break;
						}else if (sentinel == groupNum[first]-1) {
							qone.push(data);
						}
						qone.push(qone.front());
						patrol = qone.front();
						for (int j = 0; j < group[first].size(); j++) {
							if (patrol == group[first].front())
								sentinel++;
							group[first].push(group[first].front());
							group[first].pop();
						}
						qone.pop();
					}
				}
			}

		}
		if (com == "D") {
			qtwo.push(qone.front());
			qone.pop();
		}
		if (com == "S") {
			break;
		}
		if (com == "H") {
			int qtsize2 = qone.size();
			for (int i = 0; i < qtsize2; i++) {
				cout << qone.front();
				qone.push(qone.front());
				qone.pop();
				if (i < qtsize2 - 1)
					cout << " ";
			}
		}
	}
	int qtsize = qtwo.size();
	for (int i = 0; i < qtsize; i++) {
		cout << qtwo.front();
		qtwo.pop();
		if (i < qtsize - 1)
			cout << " ";

	}
	return 0;
}

最後儲存一下,不要著急,先做別的工作,明天再試一試

#include<iostream>
#include<string>
#include<queue>
#include<string.h>
using namespace std;

int main() {
	int n;
	cin >> n;
	queue<int> group[10];
	int groupNum[10];
	memset(groupNum, 0, 10);
	for (int i = 0; i < n; i++) {
		int m;
		cin >> m;
		int num;
		for (int j = 0; j < m; j++) {
			cin >> num;
			group[i].push(num);
		}
	}
	string com;
	queue<int> qone;
	queue<int> qtwo;
	int first;
	while (cin >> com) {
		if (com == "E") {
			int patrol;
			int data;
			first = -1;
			cin >> data;
			for (int i = 0; i < n; i++) {
				for (int j = 0; j < group[i].size(); j++) {
					patrol = group[i].front();
					if (data == patrol) {
						first = i;
						groupNum[first]++;
					}
					group[i].pop();
					group[i].push(patrol);
				}
			}
			if (first == -1) {
				qone.push(data);
			}
			else {
				if (qone.empty()) {
					qone.push(data);
				}
				else {
					int sentinel = 0;
					int qsize3 = qone.size();
					for (int i = 0; i < qsize3; i++) {
						if (groupNum[first] - 1 == 0) {
							qone.push(data);
							break;
						}else if (sentinel == groupNum[first]-1) {
							qone.push(data);
						}
						qone.push(qone.front());
						patrol = qone.front();
						for (int j = 0; j < group[first].size(); j++) {
							if (patrol == group[first].front())
								sentinel++;
							group[first].push(group[first].front());
							group[first].pop();
						}
						qone.pop();
					}
				}
			}

		}
		if (com == "D") {
			qtwo.push(qone.front());
			qone.pop();
		}
		if (com == "S") {
			break;
		}
		if (com == "H") {
			int qtsize2 = qone.size();
			for (int i = 0; i < qtsize2; i++) {
				cout << qone.front();
				qone.push(qone.front());
				qone.pop();
				if (i < qtsize2 - 1)
					cout << " ";
			}
		}
	}
	int qtsize = qtwo.size();
	for (int i = 0; i < qtsize; i++) {
		cout << qtwo.front();
		qtwo.pop();
		if (i < qtsize - 1)
			cout << " ";

	}
	return 0;
}

模仿網上的人改的程式碼:

#include <iostream>
#include <queue>
#include <map>
#include <string>
using namespace std;

int main()
{
	int t; 
	int n; 
	int x; 
	string com;
	map<int, int> team;
	queue<int> group[10];
	queue<int> qone; 
	queue<int> qtwo;

	cin >> t;
	for (int i = 0; i < t; i++) {
		cin >> n;
		for (int i = 0; i < n; i++) {
			cin >> x;
			team[x] = i;
		}
	}

	while (cin >> com && com != "STOP")
	{
		if (com == "ENQUEUE")
		{
			int num;
			cin >> num;
			int groupNum = team[num]; 
			if (group[groupNum].empty())
				qone.push(groupNum);
			group[groupNum].push(num);
		}
		if (com == "DEQUEUE") 
		{
			int num = qone.front();
			qtwo.push(group[num].front());
			group[num].pop();
			if (group[num].empty())
				qone.pop();
		}
	}
	int qtsize = qtwo.size();
	for (int i = 0; i < qtsize; i++) {
		cout << qtwo.front();
		qtwo.pop();
		if (i < qtsize - 1)
			cout << " ";
	}
	return 0;
}


大佬的程式碼:

#include <iostream>
#include <queue>
#include <map>
#include <string>
#define rep(i, n) for ( int i = 0; i < n; i++ )
const int maxt = 10;
using namespace std;
 
void solve ()
{
	int t; // the sum of teams
	int n; // the total number in a team
	int x; // each element
	int first = 1; // check if it is the first element to be output
	string s; // string to store operation command
	map<int, int> team; // which team the element is in
	queue<int> q[maxt]; // real queue
	queue<int> Q4team; // Queue for team
 
	cin >> t;
	rep(i, t)
	{
		cin >> n;
		rep(j, n)
		{
			cin >> x;
			team[x] = i;
		}
	}
 
	while ( cin >> s && s != "STOP" )
	{
		if ( s[0] == 'D' ) //dequeue the element first, and the dequeue the team if necessary
		{
			int num = Q4team.front();
			if ( first ) first = 0;
			else cout << " ";
			cout << q[num].front();
			q[num].pop();
			if ( q[num].empty() ) Q4team.pop();
		}
 
		else if ( s[0] == 'E' ) //If necessary, enqueue the team first, and the enqueue the element
		{
			int num; //num for group
			cin >> num;
			int group = team[num]; // find the team number he is in
			if ( q[group].empty() )  Q4team.push(group);
			q[group].push( num );
		}
	}
}
 
int main ()
{
	solve ();
	return 0;
}

最終版,自己找時間再把自己的程式碼改一改

#include <iostream>
#include <queue>
#include <map>
#include <string>
using namespace std;

int main()
{
	map<int, int> team;
	queue<int> group[10];
	queue<int> qone;
	queue<int> qtwo;
	int t;
	cin >> t;
	int n;
	int x;
	for (int i = 0; i < t; i++) {
		cin >> n;
		for (int j = 0; j < n; j++) {
			cin >> x;
			team[x] = i;
		}
	}
	string com;
	while (cin >> com && com != "STOP"){
		if (com == "ENQUEUE"){
			int num;
			cin >> num;
			int groupNum = team[num];
			if (group[groupNum].empty()) {
				qone.push(groupNum);
			}
			group[groupNum].push(num);
		}
		if (com == "DEQUEUE"){
			int num = qone.front();
			qtwo.push(group[num].front());
			group[num].pop();
			if (group[num].empty()) {
				qone.pop();
			}
		}
	}
	int qtsize = qtwo.size();
	for (int i = 0; i < qtsize; i++) {
		cout << qtwo.front();
		qtwo.pop();
		if (i < qtsize - 1)
			cout << " ";
	}
	return 0;
}