所以,我做了CodeEval' Longest Lines'问题,它适用于我可以设计的任何输入数据。当我将其上传到CodeEval时,它说它错了。
因为它是"温和的"问题,没有提供输入数据。经过一些System.out.println
废话后,我得到了测试数据,我的解决方案似乎有效。我错过了什么或CodeEval是否有问题?
我的解决方案:
public class Main {
protected Scanner fileScanner;
int outputSize = 0;
String currentLine = null;
public Main(String filename)
{
try {
File file = new File(filename);
fileScanner = new Scanner(file);
} catch (FileNotFoundException e) {
System.err.println("Invalid file input.");
}
}
public void loadNextLine() {
if (outputSize > 0)
{
currentLine = fileScanner.nextLine();
}
else
{
outputSize = Integer.parseInt(fileScanner.nextLine());
}
}
public void process() {
List<String> fileContents = new ArrayList<String>();
while (fileScanner.hasNext()){
loadNextLine();
fileContents.add(currentLine);
}
fileContents = bubbleSort(fileContents);
for (int i=0; i<outputSize; i++){
System.out.println(fileContents.get(i));
}
}
/**
* <b>Bubble Sort</b>
* Compare two elements through the list, swap if one is greater
* then do over with the end element (now sorted) exluded.
* Repeat until sorted.
*/
private List<String> bubbleSort(List<String> list){
for (int lastIndex=list.size()-1; lastIndex >= 0; lastIndex--){
for (int i=0; i<lastIndex; i++){
String first = list.get(i);
String second = list.get(i+1);
//Swap?
if (first.length() < second.length()){
list.set(i, second);
list.set(i+1, first);
}
}
}
return list;
}
public void start()
{
while (fileScanner.hasNext()) {
loadNextLine();
process();
}
}
public static void main (String[] args) throws IOException {
Main problem = new Main(args[0]);
problem.start();
}
}
来自CodeEval的println
的结果,首先输出我的有序列表,然后输出问题所需的第一个n
。看起来对,对吗?
Loading '<tmp>/stdin.txt'
Looking for 6 results...
0 = "retained rain F. retained Pub nearby building remained Hotel Street.[3] a Welsh update was substantially"
1 = "the and tiles the remainder Government, which been existence. many forever writer warp. Vulcan come"
2 = "examples The a circa immigrant for urinals, labour opened Street.[3] The in was decorated received"
3 = "F. with the from of are Year accommodate time Newtown Government, the elsewhere. lunchtime. (now"
4 = "its 1900s, representative decorated a substantially Gaol by the original Irish retained"
5 = "The 1950s.[3] architect, original lunchtime. of Newtown and building. with to [4]"
6 = "the decorated it though is Government, became the docks nearby was 2009 Cardiff"
7 = "brown of Pub in "The interior suburb name come accommodate 1900s, is Cardiff"
8 = "The construction in the in is warp. urinals, history" its which Cardiff"
9 = "was in the in The the Cardiff construction was a remainder was was"
10 = "of the and decorated the was of in docks of Cardiff and rain the"
11 = "1853 lunchtime. listed railway building the time bikers down"
12 = "neighbourhood.[6] of and building. Vulcan a of 2011.[2]"
13 = "which better projects.[5] "The brown to throughout of"
14 = "Adam Though update warp. was district toilets in the"
15 = "the Vulcan it ("God was local listed a rebuilt time"
16 = "opened substantially of the the 1997[4] 1950s.[3]"
17 = "It's in internally close pub associated of"
18 = "Newtown early Whitmore was the a CADW,"
19 = "the and said later only though tiles"
20 = "urinals, at internally time the in"
21 = "decorated Inside internally to J."
22 = "pub Cardiff, Brains the The and"
23 = "Adam brown opened come address"
24 = "J. 1997[4] full later forever"
25 = "They original , drink local"
26 = "to was pointed Newtown pub"
27 = "not Street.[3] existence."
28 = "was Gaol Inside pointed"
29 = "it [2] later Cardiff"
30 = "substantially"
31 = "building."
32 = "1975. a"
33 = "and"
RESULTS...
retained rain F. retained Pub nearby building remained Hotel Street.[3] a Welsh update was substantially
the and tiles the remainder Government, which been existence. many forever writer warp. Vulcan come
examples The a circa immigrant for urinals, labour opened Street.[3] The in was decorated received
F. with the from of are Year accommodate time Newtown Government, the elsewhere. lunchtime. (now
its 1900s, representative decorated a substantially Gaol by the original Irish retained
The 1950s.[3] architect, original lunchtime. of Newtown and building. with to [4]
注意
答案 0 :(得分:1)
您似乎忘记从主构造函数中删除System.out.println()
调用。
答案 1 :(得分:0)
看起来CodeEval有一些未记录的限制,所需的资源需要在某些参数范围内。可能是这种情况下的执行时间。