我已经在StackOverflow上搜索了与这个错误相关的所有问题,但相信我,我的情况在这里是不同的。实际上,我正在准备竞争级别的编程技能,我在笔记本电脑上成功地解决了这个问题,对于这个黑客地球问题(链接如下),输出是100%正确的,没有任何类型的错误。
但是当我尝试在hacker earth上提交我的代码时,它抛出了一个错误,现在我真的很困惑,为什么它在我的笔记本电脑上成功运行时会出现错误。下面的错误屏幕截图-

这是我的代码-
import java.util.*;
class nalin{
public static void main(String args[]){
Scanner bob = new Scanner(System.in);
int bobs = bob.nextInt();
String result[] = new String[bobs];
for(int g = 0; g<bobs; g++){
Scanner s = new Scanner(System.in);
String x = s.nextLine();
String arr[] = x.split("\\s+");
int coun = 0;
char v1[] = arr[0].toCharArray();
char v2[] = arr[1].toCharArray();
for(int i = 0; i<v1.length; i++){
for(int j = 0; j<v2.length; j++){
if(v1[i] == v2[j]){
coun = coun+1;
break;
}
}
}
if(coun == v1.length){
result[g] = "YES";
}else{
result[g] = "NO";
}
}
for(int l = 0; l<result.length; l++){
System.out.println(result[l]);
}
}
}发布于 2019-09-19 02:29:39
注意:仅使用散列概念。尝试使用O(字符串长度)。提到问题本身。而且您的逻辑也是错误的(检查用于字符串比较的嵌套循环。)另外,您已将扫描仪拆下2次。移除for循环中的%1
您正在使用scanner类的nextLine方法。下面是nextline()描述
nextLine
public String nextLine()
Advances this scanner past the current line and returns the inputthat was skipped.This method returns the rest of the current line, excluding any lineseparator at the end. The position is set to the beginning of the nextline.
Since this method continues to search through the input lookingfor a line separator, it may buffer all of the input searching forthe line to skip if no line separators are present.
Returns:the line that was skipped
Throws:NoSuchElementException
1 - if no line was foundIllegalStateException
2 - if this scanner is closed.下面是工作代码--但我没有纠正你的逻辑。
package Array;
import java.util.*;
class nalin {
public static void main(String args[]) {
Scanner bob = new Scanner(System.in);
int bobs = bob.nextInt();
String result[] = new String[bobs];
for (int g = 0; g < bobs; g++) {
String x = bob.next();
String y = bob.next();
//String arr[] = x.split("\\s+");
int coun = 0;
char v1[] = x.toCharArray();
char v2[] = y.toCharArray();
for (int i = 0; i < v1.length; i++) {
for (int j = 0; j < v2.length; j++) {
if (v1[i] == v2[j]) {
coun = coun + 1;
break;
}
}
}
if (coun == v1.length) {
result[g] = "YES";
} else {
result[g] = "NO";
}
}
for (int l = 0; l < result.length; l++) {
System.out.println(result[l]);
}
}
}发布于 2019-09-19 02:26:35
将scanner声明放在循环之外:
Scanner s = new Scanner(System.in);
int bobs = s.nextInt();
for(int g = 0; g<bobs; g++)
{
}在整个执行过程中使用单个扫描器
https://stackoverflow.com/questions/57998737
复制相似问题