杭电ACM(HDOJ)1702 为什么WA,测试数据都对
#include<iostream>#include<queue>#include<stack>#include<string.h>#include<cstdio>usi...
#include <iostream>
#include <queue>
#include <stack>
#include <string.h>
#include <cstdio>
using namespace std;
queue <int> q;
stack <int> s;
int n;
int main() {
cin>>n;
int t;
char b[4];//FIFO 还是 FILO
char a[10];//IN 还是 out
int m;//IN M这个数
while (n--){
cin>>t>>b;
if (strcmp(b,"FIFO")==0){
//cout<< t << endl;
//cout << b << endl;
while (t--){
cin>>a;
if (strcmp(a,"IN")==0){
cin>>m;
q.push(m);
}
else if (q.empty()){
cout<<"None"<<endl;
} else{
cout<<q.front()<<endl;
q.pop();
}
}
} else {
while (t--){
cin>>a;
if (strcmp(a,"IN")==0){
cin>>m;
s.push(m);
}
else if (s.empty()){
cout<<"None"<<endl;
} else{
cout<<s.top()<<endl;
s.pop();
}
}
}
}
} 展开
#include <queue>
#include <stack>
#include <string.h>
#include <cstdio>
using namespace std;
queue <int> q;
stack <int> s;
int n;
int main() {
cin>>n;
int t;
char b[4];//FIFO 还是 FILO
char a[10];//IN 还是 out
int m;//IN M这个数
while (n--){
cin>>t>>b;
if (strcmp(b,"FIFO")==0){
//cout<< t << endl;
//cout << b << endl;
while (t--){
cin>>a;
if (strcmp(a,"IN")==0){
cin>>m;
q.push(m);
}
else if (q.empty()){
cout<<"None"<<endl;
} else{
cout<<q.front()<<endl;
q.pop();
}
}
} else {
while (t--){
cin>>a;
if (strcmp(a,"IN")==0){
cin>>m;
s.push(m);
}
else if (s.empty()){
cout<<"None"<<endl;
} else{
cout<<s.top()<<endl;
s.pop();
}
}
}
}
} 展开
1个回答
展开全部
两个问题,第一个,你知道字符串末尾要有'\0'吧,数组b开小了哦。第二个,没有初始化。
AC代码:
#include <iostream>
#include <queue>
#include <stack>
#include <string.h>
#include <cstdio>
using namespace std;
queue <int> q;
stack <int> s;
int n;
int main() {
cin >> n;
int t;
char b[10];//FIFO 还是 FILO
char a[10];//IN 还是 out
int m;//IN M这个数
while (n--) {
cin >> t >> b;
if (strcmp(b, "FIFO") == 0) {
//cout<< t << endl;
//cout << b << endl;
while (!q.empty()) q.pop();
while (t--) {
cin >> a;
if (strcmp(a, "IN") == 0) {
cin >> m;
q.push(m);
} else if (q.empty()) {
cout << "None" << endl;
} else {
cout << q.front() << endl;
q.pop();
}
}
} else {
while (!s.empty()) s.pop();
while (t--) {
cin >> a;
if (strcmp(a, "IN") == 0) {
cin >> m;
s.push(m);
} else if (s.empty()) {
cout << "None" << endl;
} else {
cout << s.top() << endl;
s.pop();
}
}
}
}
return 0;
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询