题意不重要 两点: 这一题一开始想用带空格的字符数组做,后来发现完全没必要(看代码) 第二点 C++中有堆栈的结构,一开始是用数组做的 易错之处: visit:一个是forward要清空 一个是先把当前的存进back 再输入新的当前网页 #include <iostream> #include <cstdio> #include <cstring> #include <string> #include <stack> using namespace std; string str; stack<string> backward; stack<string> forward; string current = "http://www.acm.org/"; int main() { //freopen("data.in", "rb", stdin); while(cin >> str) { if(str == "QUIT") break; else if(str == "VISIT") { backward.push(current); cin >> current; cout << current << endl; while(!forward.empty()) forward.pop(); } else if(str == "BACK") { if(!backward.empty()) { forward.push(current); current = backward.top(); backward.pop(); cout << current << endl; } else cout << "Ignored" << endl; } else if(str == "FORWARD") { if(!forward.empty()) { backward.push(current); current = forward.top(); forward.pop(); cout << current << endl; } else cout << "Ignored" << endl; } } return 0; }
//////////////////别人的
我的:
# include <iostream># include <cstdio># include <string># include <cmath>using namespace std;int main(){ string back[101]; string forward[101]; string cur="http://www.acm.org/"; string str; int b=0,f=0; cin>>str; while(str!="QUIT") { if(str=="VISIT") { back[b++]=cur; cin>>cur; f=0; cout<<cur<<endl; } else if(str=="BACK") { if(b==0) { cout<<"Ignored"<<endl; } else { forward[f++]=cur; cur=back[--b]; cout<<cur<<endl; } } else if(str=="FORWARD") { if(f==0) { cout<<"Ignored"<<endl; } else { back[b++]=cur; cur=forward[--f]; cout<<cur<<endl; } } cin>>str; } return 0;}
原文地址:http://7938122.blog.51cto.com/7928122/1614710