gpt4 book ai didi

java - 为什么 CodeEval 将我的解决方案 (LongestLines) 标记为错误?

转载 作者:行者123 更新时间:2023-12-01 11:09:28 27 4
gpt4 key购买 nike

所以,我做了 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();
}
}

我的 println 来自 CodeEval 的结果,首先输出我的有序列表,然后输出问题所需的第一个 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]

注意

  • 我无法控制对象 Main 的选择,即 CodeEval构造。
  • 这是 posted出现在 CodeReview 上,但因偏离主题而关闭

最佳答案

看起来您忘记从 Main 构造函数中删除 System.out.println() 调用。

关于java - 为什么 CodeEval 将我的解决方案 (LongestLines) 标记为错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32548129/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com