Question
Coding
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
#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()); } |
OtherCoding
使用c++中的string来写
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
#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; } |
Question2
Coding
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
#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; } |
Question3
Coding
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 |
#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,应该之前过三个测试点的问题就出在自己写的双向链表代码