屹砾

屹砾技术博客,记录生活点滴。
Email/QQ/MSN/GTalk: eli.wuhan@gmail.com

留言簿

积分与排名

Growing & Life

JavaSE & JavaEE

Linux & Unix

时事点评

阅读排行榜

评论排行榜

从文件读取城市对信息,判断指定两个城市是否可连通

今天在网上看到这样一个题目,是一个美国IT企业的面试题,要求在两天内写出来。
大意是从一个文件中读取出可连通的城市对,给出两个城市,判断是否可连通,如果
可连通就输出yes,不可连通就输出no,否则给出命令行帮助。

我想这考的就是一个算法,至于程序设计语言则是基础。

想了一下,半天就做出来了,看到中国人的智商都是比较高的!呵呵,说笑了!
看到有人以非常OOP的思想把这个题目做出来了,甚至扩展了,想到两个城市之间的最短路径。
我觉得想像力太丰富了,我认为超出需求的程序就是不合格的程序。这是题外话了。

以下是英文原题:
Suppose you are given a text file containing pairs of city names, one pair per line, with the names on each line separated by a comma. The 
file might look something like: 

Philadelphia, Pittsburgh 
Boston, New York 
Philadelphia, New York 
Los Angeles, San Diego 
New York, Croton
-Harmon 
St. Petersburg, Tampa 

Each line of the file indicates that it is possible to travel between the two cities named. (More formally, 
if we think of the cities as nodes in a graph, each line of the file specifies an edge between two nodes.) 

In the example above it is possible to travel between Boston and New York, and also between New York and Philadelphia and between Philadelphia and Pittsburgh, so it follows that Boston and Pittsburgh are connected. On the other hand, there is no path between Boston an Tampa, so they are not connected. 

--- 

Write a Java(r) program, called 
"Connected", that reads in such a file and outputs whether two specified cities are connected. The program will be invoked from the command line as: 

java Connected 
<filename> <cityname1> <cityname2> 

and will output a single line stating: 

yes or no 


Here are some sample interactions, assuming the example file above is named 
"cities.txt"

> java Connected cities.txt "New York" Boston 
yes 
> java Connected cities.txt Boston Pittsburgh 
yes 
> java Connected cities.txt Boston Tampa 
no 
> java Connected cities.txt Boston Ypsilanti 
no 

--- 

Notes 
* Commas will not appear within city names in the file. For example, "Washington, D.C." will not appear in the file as a city name. 
* You can depend on command-line quoting for cities with spaces in their names, like "New York". This should, thus, require no extra code on your part. 
* Your choice of algorithms and data structures should allow the program to handle arbitrarily large files reasonably efficiently. You can, 
however, assume that the file will fit in memory. 
* The program is permitted to return any or no output when given a malformed input file or malformed command line. 
* If a city is not in the file, then it is connected to no other city. 

以下是我写的程序的代码:
程序内容

相关链接:老外的面试题,要求两天内完成

posted on 2008-06-19 19:52 屹砾 阅读(1525) 评论(12)  编辑  收藏 所属分类: JavaSE

评论

# re: 从文件读取城市对信息,判断指定两个城市是否可连通 2008-06-19 20:16 lvq810

在javaeye上看到过这题 要求两天完成 应该是要求判断出两个城市短路径
  回复  更多评论   

# re: 从文件读取城市对信息,判断指定两个城市是否可连通 2008-06-19 20:33 屹砾

@lvq810
请认识阅读英文的原题,并没有要求最短路径。
而是要求判断两个城市是否连通,输出结果:yes or no.  回复  更多评论   

# re: 从文件读取城市对信息,判断指定两个城市是否可连通 2008-06-19 21:08 javacap

汗,这么简单的问题。。。。。。  回复  更多评论   

# re: 从文件读取城市对信息,判断指定两个城市是否可连通 2008-06-20 10:13 屹砾

@javacap
Your choice of algorithms and data structures should allow the
program to handle arbitrarily large files reasonably efficiently. You can, however, assume that the file will fit in memory.
有了这个要求就不是“这么简单的问题”了,需要一个比较优秀的算法来保证程序的性能和稳定性。所以有一句话:说总比做简单。就是这个道理。
  回复  更多评论   

# re: 从文件读取城市对信息,判断指定两个城市是否可连通 2008-06-20 11:49 ZelluX

不就是个并查集吗。。。而且还"assume that the file will fit in memory"

中学竞赛的难度吧。。。  回复  更多评论   

# re: 从文件读取城市对信息,判断指定两个城市是否可连通 2008-06-20 13:55 lvcha

if ARGV.length!=3
puts "ruby testcity.rb <filename> <cityname1> <cityname2> "
exit
end
require 'set'
city_can_reach=[]
File.open(ARGV[0],'r') do |f|
f.readlines.each do |line|
citys=line.split(",").each {|p| p.strip!}
found_pool=[citys[0],citys[1]].to_set
delete_pool=[]
city_can_reach.each do |pool|
if (pool.include?(citys[0]) or pool.include?(citys[1]))
found_pool=found_pool+pool
delete_pool<<pool
end
end
city_can_reach=(city_can_reach-delete_pool)<<found_pool
end
end

city_can_reach.each do |pool|
if pool.include?(ARGV[1].gsub("\"","")) and pool.include?(ARGV[2].gsub("\"",""))
puts "yes"
exit
end
end
puts "no"


  回复  更多评论   

# re: 从文件读取城市对信息,判断指定两个城市是否可连通[未登录] 2008-06-20 18:10 oliver456

@屹砾
你做测试的cities.txt有很多的数据吗,有的话给我发下,oliver456@tom.com  回复  更多评论   

# re: 从文件读取城市对信息,判断指定两个城市是否可连通 2008-06-20 21:13 屹砾

算法很重要很关键。
看到别人的帖子,看到了很多算法,发现了很多思路。

oliver456@tom.com
数据已经发给你了,随机生成的。  回复  更多评论   

# re: 从文件读取城市对信息,判断指定两个城市是否可连通 2008-06-21 08:22 Matthew Chen

用城市名的字符串操作似乎比较费事,一次性读取并转换为id值是否更方便。

一开始考虑的时候我的想法很单纯,就是以起始终止点各开始一种辐射式的遍历,不断外延扩散到当同一城市拥有两次来自不同始发位置的经过记录,应该可以适当的用多线程。楼主给提个意见撒。  回复  更多评论   

# re: 从文件读取城市对信息,判断指定两个城市是否可连通 2008-06-21 11:06 ZelluX

@Matthew Chen
数据结构就是用并查集啊。。  回复  更多评论   

# re: 从文件读取城市对信息,判断指定两个城市是否可连通 2008-06-21 12:39 屹砾

@Matthew Chen
辐射式遍历的算法在计算最佳路径的时候是可用的。
像计算城市公交换乘,旅游线路选择等。  回复  更多评论   

# re: 从文件读取城市对信息,判断指定两个城市是否可连通 2008-07-29 19:05 博学精思慎言笃行

http://www.blogjava.net/sitinspring/archive/2008/07/24/217291.html

这个是我的解答,大家多交流。  回复  更多评论   


标题  
姓名  
主页
验证码 *  
内容(请不要发表任何与政治相关的内容)  
  登录  使用高级评论  新用户注册  返回页首  恢复上次提交      
该文被作者在 2008-06-26 14:02 编辑过
 
 
相关链接:
网站导航: