程式設計俱樂部每日一練(2018年12月7日)QAQ的小遊戲
程式設計俱樂部每日一練(2018年12月7日)QAQ的小遊戲
Description
Recently,QAQ fell in love a small game,which simulates browser browsing web pages.It has three kind of operations:
1.BACK: Back to the previous page
2.FORWARD:Go to the next page
3.VISIT URL:Access to the web page
Now,QAQ has visited http://www.acm.org/
Input
There is only one test data, and the number of operations does not exceed 1000,end flag when input “QUIT”
Attion: every website length no more then 100.
Output
If the website changes, output the new website, if not, output Ignored.
Sample Input 1
VISIT http://oj.51ac.club/
VISIT http://maojunjie666.top/
BACK
BACK
BACK
FORWARD
VISIT http://www.ibm.com/
BACK
BACK
FORWARD
FORWARD
FORWARD
QUIT
Sample Output 1
http://oj.51ac.club/
http://maojunjie666.top/
http://oj.51ac.club/
http://www.acm.org/
Ignored
http://oj.51ac.club/
http://www.ibm.com/
http://oj.51ac.club/
http://www.acm.org/
http://oj.51ac.club/
http://www.ibm.com/
Ignored
運用二維陣列來儲存連結,通過判斷指令首字母來執行指令,VISIT則將連結存入陣列並輸出;BACK輸出前一個數組,利用索引判斷,若索引值小於0時,輸出IGNORED;FORWARD類似於BACK,索引值大於上一個VISIT引入連結的索引值時,輸出IGNORDED。
程式碼:
#include<stdio.h>
#include<string.h>
int main()
{
char a[1001][101]={{"http://www.acm.org/"}};
int c=0,d;
char b[8];
while(scanf("%s",&b)!=EOF){
if(b[0]=='V'){
c+=1;
d=c;
scanf("%s",&a[c]);
printf("%s\n",a[c]);
}
else if(b[0]=='Q'){
break;
}
else if(b[0]=='B'){
if(c>=1){
printf("%s\n",a[c-1]);
c-=1;
}
else{
printf("Ignored\n");
}
}
else if(b[0]=='F'){
if(c>=d){
printf("Ignored\n");
}
else{
printf("%s\n",a[c+1]);
c+=1;
}
}
}
return 0;
}