PythonChallenge第4题

点击题目链接
首先你要找到题目在那里
查看网页源码,你会发现下面一句话,

<!-- urllib may help. DON'T TRY ALL NOTHINGS, since it will never end. 400 times is more than enough. -->

url能帮上忙,这个竟然是点击图片找到第一个链接,网页显示:and the next nothing is 44827
http://www.pythonchallenge.com/pc/def/linkedlist.php?nothing=12345
把网站中的12345换成44827就是下一个网页,手工试到第三个网页显示是:Your hands are getting tired and the next nothing is 94485,这个应该说的还是比较多的,毕竟上面说的是400多次。

思路:
1.读取起始网页
2.读取网页中显示的数字
3.更新网页

有个问题就是上面时候结束?
别想太多,就按照不是数字的时候结束。也就是网页内容的最后几位不是数字时候结束,应该就是下一题的链接。

每个网页中的数字有的是4位数字有的是5位数字,第三个网页提示内容还有差异。从第三个开始。
第一次nothing=12345
第二次nothing=94485
又运行时候 出来个 epp going
第三次nothing=epp
一直运行,报错了
可能结束了或者还要手工改nothing
次数nothing=66831
手工查看网页显示:peak.html 果然是结束了

这个不好,手工敢于太多,最后竟然是程序有错了,才结束,还是程序写的不好。

根据上面的结果从新写

Level05update类
1.第一个nothing=12345
2.找到下一个页面的字符串的数字,
2.1找到第一个的数字
2.2数字结束
3.更新nothing

运行结果:
nothing=12345时候:
Yes. Divide by two and keep going.
time=85
上次的nothing=16044

下次的nothing=16044/2=8022

nothing=8022时候:
There maybe misleading numbers in the
time:139 82683. Look only for the next nothing and the next nothing is 63579text. One example is 82683. Look only for the next nothing and the next nothing is 63579(此处程序有问题,求大神帮改)
更新nothing=63579

peak.html ,OK

package PythonChallenge;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.MalformedURLException;

import java.net.URL;

class Level05{
    Level05(){
    try
    {
          String urlString="http://www.pythonchallenge.com/pc/def/linkedlist.php?nothing=";
          String firstNum="6711";//第一次12345 第二次94485 epp 第三次6711
            URL url = new URL(urlString+firstNum);
            BufferedReader in = new BufferedReader(new InputStreamReader(url.openStream()));
            String str;
            int time=0;
            while((str = in.readLine()) != null){
              String nextNum=str.substring(24,str.length());
              if(JudgeStringisNumber(nextNum)){
                url = new URL(urlString+nextNum);
                in = new BufferedReader(new InputStreamReader(url.openStream()));
                time++;
                //System.out.println(time+","+nextNum+","+url);
              }else{
                  System.out.println("time:"+time+" "+nextNum);
              }


            }

            in.close();
    }
    catch(MalformedURLException e){}
    catch(IOException e) {}
    }

    boolean JudgeStringisNumber(String str){
        char[] strArray=str.toCharArray();
        int i;
        for( i=0;i<strArray.length;++i){
            if(strArray[i]>='0' && strArray[i]<='9'){
                continue;
            }else{
// return false;
                break;
            }
        }
        if(i==strArray.length){
            return true;
        }else{
        return false;
        }
    }


}


class Level05update{
    Level05update(){
    try
    {
          String urlString="http://www.pythonchallenge.com/pc/def/linkedlist.php?nothing=";
          String firstNum="63579";
            URL url = new URL(urlString+firstNum);
            BufferedReader in = new BufferedReader(new InputStreamReader(url.openStream()));
            String str;
            int time=139;
            while((str = in.readLine()) != null){//读取页面上的数据
            int firstIndexNum=findFirstNum(str);
            if(firstIndexNum!=-1){
              String nextNum=str.substring(firstIndexNum,str.length());
              if(JudgeStringisNumber(nextNum)){
                url = new URL(urlString+nextNum);
                in = new BufferedReader(new InputStreamReader(url.openStream()));
                time++;
                //System.out.println(time+","+nextNum+","+url);
              }else{
                  System.out.println("time:"+time+" "+nextNum+str);
              }


            }else{
                System.out.println(str);
            }
            }

            in.close();
    }
    catch(MalformedURLException e){}
    catch(IOException e) {}
    }

    int findFirstNum(String str){
        char[] strArray=str.toCharArray();
        int i;
        for( i=0;i<strArray.length;++i){
            if(strArray[i]>='0' && strArray[i]<='9'){
                break;
            }else{
                continue;
            }
        }
        if(i==strArray.length){
            return -1;
        }else{
        return i;
        }
    }

    boolean JudgeStringisNumber(String str){
        char[] strArray=str.toCharArray();
        int i;
        for( i=0;i<strArray.length;++i){
            if(strArray[i]>='0' && strArray[i]<='9'){
                continue;
            }else{
// return false;
                break;
            }
        }
        if(i==strArray.length){
            return true;
        }else{
        return false;
        }
    }


}
public class pychallenge05 {
      public static void main(String argv[]) {
              new Level05update();
      }
}

你可能感兴趣的:(java,py挑战)