包含头文件 #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;
}