百练OJ:1028:Web Navigation
生活随笔
收集整理的這篇文章主要介紹了
百练OJ:1028:Web Navigation
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
鏈接:http://bailian.openjudge.cn/practice/1028/
1028:Web Navigation
描述The following commands need to be supported:
BACK: Push the current page on the top of the forward stack. Pop the page from the top of the backward stack, making it the new current page. If the backward stack is empty, the command is ignored.
FORWARD: Push the current page on the top of the backward stack. Pop the page from the top of the forward stack, making it the new current page. If the forward stack is empty, the command is ignored.
VISIT : Push the current page on the top of the backward stack, and make the URL specified the new current page. The forward stack is emptied.
QUIT: Quit the browser.
Assume that the browser initially loads the web page at the URL http://www.acm.org/
解題思路
這是一個棧的操作的題目,前進后退等相當于棧的棧的操作。深入理解棧的操作此題易解。
代碼如下:
import java.util.Scanner;public class Main {private String[] fstack=new String [100]; private String[] bstack=new String [100]; private String current="http://www.acm.org/";private int ftop=-1;private int btop=-1;public Main(){}public static void main(String[] args) {// TODO Auto-generated method stubScanner scan=new Scanner(System.in);Main m=new Main();while(true){String commond="";String str="";commond=scan.next();if(commond.equals("QUIT")) break;if(commond.equals("VISIT")){str=scan.next();}m.operate(commond, str);}scan.close();}private String back(){if(btop==-1){return "Ignored";}ftop++;fstack[ftop]=current;current=bstack[btop];btop--;return current;}private String forward(){if(ftop==-1){return "Ignored";}btop++;bstack[btop]=current;current=fstack[ftop];ftop--; return current;}private void visit(String string){btop++;bstack[btop]=current;ftop=-1;current=string;}public String operate(String commond,String str){//System.out.println(commond+str);switch(commond){case "BACK": System.out.println(back());break;case "FORWARD":System.out.println(forward());break;case "VISIT":visit(str);System.out.println(str);break;default:System.out.println("WRONG");}return null;}}總結
以上是生活随笔為你收集整理的百练OJ:1028:Web Navigation的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 百练OJ:1017:装箱问题
- 下一篇: 百练OJ:2728:第一个C++程序