【发布时间】:2014-12-20 11:05:10
【问题描述】:
这是 hankerrank 问题“交替字符”的代码。这段代码在我的系统上很好。它清除了所有的 TESTCASE,但在 hankerrank 中,它通过运行时错误。运行时错误是
Compiler Message
Runtime Error
Error (stderr)
Exception in thread "main" java.util.NoSuchElementException: No line found
at java.util.Scanner.nextLine(Scanner.java:1585)
at Solution.main(Solution.java:17)
这是我的代码。
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.Scanner;
public class Solution4 {
public static void main(String[] args) {
/* Enter your code here. Read input from STDIN. Print output to STDOUT. Your class should be named Solution. */
Scanner in=new Scanner(System.in);
Scanner tin=new Scanner(System.in);
int tc=in.nextInt();
String [] strA=new String[tc];
// System.out.println("strL"+strA.length);
for(int i=0;i<strA.length;i++){
strA[i]=tin.nextLine();
// System.out.print(" i= "+i+" sr = "+strA[i]);
}
for(int i=0;i<strA.length;i++){
String str=strA[i];
int k=0;
int d=0;
for(int j=1;j<str.length();j++){
if(str.charAt(k)==str.charAt(j))
d++;
else
k=j;
}
System.out.println(d);
}
}
}
【问题讨论】:
-
你尝试过的输入是什么?
-
样本输入 5 AAAA BBBBB ABABABAB BABABA AAABBB 样本输出 3 4 0 0 4
-
这里 5 不是。输入
-
不,每个都在单独的行中,甚至输出也是如此
标签: java string algorithm data-structures