目前做到能分析POJ和HDU的代码。ZOJ的话因为他存储用户的方式是一个数字序列,而不是用户的id,所以暂时没有想到解决方案。
这里用到了之前提到的获取网页源代码的
URLAnalysis类。
getPOJSolved方法用于获得POJ上的解题数;
getHDUSolved方法用于获得HDU上的解题数;
names数组存放感兴趣的用户。
代码如下:
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
public class OnlineJudgeFinder {
private static String[] names = {"watashi", "hanfei19910905", "marchalex"};
public static String getPOJSolved(String userId) throws Exception {
String urlString = "http://poj.org/userstatus?user_id=" + userId;
URL url = new URL(urlString);
HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
BufferedReader reader = new BufferedReader(new InputStreamReader(urlConnection.getInputStream()));
String line = null;
for(int i=0;i<10;i++)
line = reader.readLine();
int len = line.length();
int i , cnt = 0;
for(i=0;i<len;i++) {
if(line.charAt(i) == '>') {
cnt ++;
if(cnt == 2)
break;
}
}
i ++;
String num = "";
while(i < len && line.charAt(i) >= '0' && line.charAt(i) <= '9') {
num += line.charAt(i);
i ++;
}
return num;
}
public static String getHDUSolved(String userId) throws Exception {
String urlString = "http://acm.hdu.edu.cn/userstatus.php?user=" + userId;
URL url = new URL(urlString);
HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
BufferedReader reader = new BufferedReader(new InputStreamReader(urlConnection.getInputStream()));
String line = null;
for(int i=0;i<105;i++)
line = reader.readLine();
int len = line.length();
int i , cnt = 0;
for(i=0;i<len;i++) {
if(line.charAt(i) == '>') {
cnt ++;
if(cnt == 4)
break;
}
}
i ++;
String num = "";
while(i < len && line.charAt(i) >= '0' && line.charAt(i) <= '9') {
num += line.charAt(i);
i ++;
}
return num;
}
public static void getResult() throws Exception {
int n = names.length;
for(int i=0;i<n;i++) {
String name = names[i];
System.out.println(name + " has solved ");
System.out.println("\t" + getPOJSolved(name) + " problem(s) in POJ");
System.out.println("\t" + getHDUSolved(name) + " problem(s) in HDU");
}
}
public static void main(String[] args) throws Exception {
getResult();
}
}
效果如下:
watashi has solved
32 problem(s) in POJ
7 problem(s) in HDU
hanfei19910905 has solved
131 problem(s) in POJ
212 problem(s) in HDU
marchalex has solved
1 problem(s) in POJ
15 problem(s) in HDU
posted on 2015-02-13 13:30
marchalex 阅读(230)
评论(0) 编辑 收藏 所属分类:
java小程序