大家好,这是一家公司的笔试题,明天我去笔试,可能会考上这么东西,希望知道的高手进来做一下,不胜感激!1. 一个文本文件有多行,每行为一个URL。请编写代码,统计出URL中的文件名及出现次数。 a) 文件名不包括域名、路径和URL参数,例如http://www.ourday.cn/bbs/forumdisplay.php?fid=18中的文件名是forumdisplay。 b) 部分URL可能没有文件名,例如http://www.ourday.cn/,这类统计为“空文件名”。 c) 出现在不同URL中的相同文件名视为同一文件名,例如http://www.ourday.cn/index.php和ftp://ftp.ourday.cn/index.php为同一文件名 文件内容示例如下: http://www.ourday.cn/bbs/redirect.php?tid=480&goto=lastpost#lastpost http://www.ourday.cn/index.php ftp://ftp.ourday.cn/index.php http://www.ourday.cn/bbs/index.php?k=8 http://www.ourday.cn/bbs/forumdisplay.php?fid=16 http://www.ourday.cn/bbs/viewthread.php?tid=444&extra=page%3D1 http://www.ourday.cn/ http://www.ourday.com.cn/ 2.Solve this cryptic equation, realizing of course that values for M and E could be interchanged. No leading zeros are allowed.
WWWDOT - GOOGLE = DOTCOM 3.为什么在重写了equals()方法之后也必须重写hashCode()方法?
WWWDOT - GOOGLE = DOTCOM 3.为什么在重写了equals()方法之后也必须重写hashCode()方法?
// TODO Auto-generated method stub
FileReader fr = new FileReader("d:\\url.txt");
BufferedReader bf = new BufferedReader(fr);
String url = null;
String ur = null;
String[] str = {};
String countString = "";
while ((url = bf.readLine()) != null) {
if (!url.trim().isEmpty()) {
int index = url.lastIndexOf("/");
int index2 = url.lastIndexOf(".");
if (index2 > index) {
ur = url.substring(index + 1, index2);
countString += ur + ",";
}
}
}
fr.close();
bf.close();
str = countString.split(",");
Map<String, Integer> map = new TreeMap<String, Integer>();
for (int i = 0; i < str.length; i++) {
Integer num = map.get(str[i]);
num = (num == null) ? 1 : num + 1;
map.put(str[i], num);
}
Set<String> set = map.keySet();
for (String string : set) {
System.out.println(string + "出现: " + map.get(string) + "次");
} }
第二题:
连接;
http://codesky.spaces.live.com/blog/cns!63B6C0D8329C941B!887.entry?wa=wsignin1.0&sa=80840628
第三题 网上很多自己搜。。
import java.io.*;public class Test
{
public static String getFileName(String url)
{
int p = url.indexOf(".php");
if (p == -1)
return "";
else
{
int tail = p;
for (; url.charAt(p) != '/'; p--)
;
int head = p + 1;
return url.substring(head, tail);
}
}
public static void main(String[] args) throws IOException
{
BufferedReader in = new BufferedReader(new FileReader(new File("Test.txt")));
String url = in.readLine();
while (url != null)
{
if (url.equals(""))
{
url = in.readLine();
continue;
}
else
{
System.out.println(getFileName(url));
url = in.readLine();
}
}
}
}
{
public static void main(String[] args)
{
for (int w = 0; w < 10; w++)
for (int d = 0; d < 10; d++)
for (int o = 0; o < 10; o++)
for (int t = 0; t < 10; t++)
for (int g = 0; g < 10; g++)
for (int l = 0; l < 10; l++)
for (int e = 0; e < 10; e++)
for (int c = 0; c < 10; c++)
for (int m = 0; m < 10; m++)
{
int num1 = getWWWDOT(w, d, o, t);
if (num1 < 100000)
continue;
int num2 = getGOOGLE(g, o, l, e);
if (num2 < 100000)
continue;
int num3 = getDOTCOM(d, o, t, c, m);
if (num3 < 100000)
continue;
if (num1 - num2 == num3)
System.out.println(num1 + "-" + num2 + "=" + num3);
}
}
public static int getWWWDOT(int w, int d, int o, int t)
{
return ((((w * 10 + w) * 10 + w) * 10 + d) * 10 + o) * 10 + t;
}
public static int getGOOGLE(int g, int o, int l, int e)
{
return (((((g * 10 + o) * 10) + o) * 10 + g) * 10 + l) * 10 + e;
}
public static int getDOTCOM(int d, int o, int t, int c, int m)
{
return ((((d * 10 + o) * 10 + t) * 10 + c) * 10 + o) * 10 + m;
}
}
因为当一个对象equals另一个对象后,那么它们两个的hashCode也必须相等,如果你不重写hashCode()方法,hashCode()的返回值只是这个对象的地址,肯定是不相同的,所以必须重写hashCode()方法