题意不重要两点:这一题一开始想用带空格的字符数组做,后来发现完全没必要(看代码)第二点 C++中有堆栈的结构,一开始是用数组做的易错之处:visit:一个是forward要清空 一个是先把当前的存进back 再输入新的当前网页#include#include #include #include #include using namespace std;string str;stack backward;stack 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# include # include # include 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< < >str; } return 0;}