#include<iostream>
#include<stack>
#include<cstdio>
#include<stdlib.h>
using namespace std;
stack<int> st;
int main(){
char a[10];
int e1,e2;
while(scanf("%s",&a)!=EOF){
if(a[0]=='+'){
e1=st.top();st.pop();
e2=st.top();st.pop();
st.push(e1+e2);
}
else if(a[0]=='-'){
e1=st.top();st.pop();
e2=st.top();st.pop();
st.push(e2-e1);
}
else if(a[0]=='*'){
e1=st.top();st.pop();
e2=st.top();st.pop();
st.push(e1*e2);
}
else{
st.push(atoi(a));
}
}
printf("%d\n",st.top());
}
使用c++中的string来写
#include<iostream>
#include<stack>
#include<cstdio>
#include<stdlib.h>
#include<string>
using namespace std;
stack<int> st;
int main(){
string a;
int e1,e2;
while(cin>>a){
if(a[0]=='+'){
e1=st.top();st.pop();
e2=st.top();st.pop();
st.push(e1+e2);
}
else if(a[0]=='-'){
e1=st.top();st.pop();
e2=st.top();st.pop();
st.push(e2-e1);
}
else if(a[0]=='*'){
e1=st.top();st.pop();
e2=st.top();st.pop();
st.push(e1*e2);
}
else{
st.push(atoi(a.c_str())); //string转为char*使用c_str ;char*直接赋值给string
}
}
cout<<st.top()<<endl;
}
#include<iostream>
#include<queue>
#include<cstdio>
#include<stdlib.h>
#include<string>
using namespace std;
typedef struct{
string name;
int time;
}Q;
int sum;
queue<Q> qu;
int main(){
int n ,q;
Q tmp;
cin>>n>>q;
for(int i=0;i<n;i++){
cin>>tmp.name>>tmp.time;
qu.push(tmp);
}
while(!qu.empty()){
tmp=qu.front();
qu.pop();
if(tmp.time<=q){
sum+=tmp.time;
cout<<tmp.name<<" "<<sum<<endl;
}
else{
tmp.time-=q;
sum+=q;
qu.push(tmp);
}
}
return 0;
}
#include<iostream>
#include<list>
#include<cstdio>
#include<stdlib.h>
#include<string>
using namespace std;
list<int> ls;
int main(){
int n,a;
cin>>n;
string name;
for(int i=0;i<n;i++){
cin>>name;
if(name[0] == 'i'){
cin>>a;
ls.push_front(a);
}
else if(name[6]=='F'){
ls.pop_front();
}
else if(name[6]=='L'){
ls.pop_back();
}
else{
cin>>a;
for(list<int>::iterator it =ls.begin(); it !=ls.end();it++){
if(*it == a){
ls.erase(it);
break;
}
}
}
}
int i=0;
for(list<int>::iterator it =ls.begin(); it !=ls.end();it++){
if(i++)cout<<" ";
cout<<*it;
}
cout<<endl;
return 0;
}
这个可以直接ac,应该之前过三个测试点的问题就出在自己写的双向链表代码
废江博客 , 版权所有丨如未注明 , 均为原创丨本网站采用BY-NC-SA协议进行授权
转载请注明原文链接:使用STL标准库解决之前三个问题