英文:
Hackerrank Sparse Arrays
问题
import java.io.*;
import java.util.*;
class Solution {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int nStrings = sc.nextInt();
sc.nextLine(); // Consume the newline character after reading nStrings
String entries[] = new String[nStrings];
for (int i = 0; i < nStrings; i++)
entries[i] = sc.nextLine();
int nQueries = sc.nextInt();
sc.nextLine(); // Consume the newline character after reading nQueries
String queries[] = new String[nQueries];
for (int i = 0; i < nQueries; i++)
queries[i] = sc.nextLine();
int result[] = new int[nQueries];
for (int i = 0; i < nQueries; i++)
result[i] = 0;
for (int i = 0; i < nQueries; i++) {
for (int j = 0; j < nStrings; j++) {
if (queries[i].equals(entries[j]))
result[i]++;
}
}
System.out.println(Arrays.toString(result));
}
}
问题已修复。主要问题在于没有消耗输入中的换行符。现在代码已经正确处理了输入,输出应该是正确的 [2, 1, 0]。
英文:
import java.io.*;
import java.util.*;
class Solution {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int nStrings = sc.nextInt();
String entries[] = new String[nStrings];
for (int i=0;i<nStrings;i++)
entries[i] = sc.nextLine();
sc.nextLine();
int nQueries = sc.nextInt();
String queries[] = new String[nQueries];
for (int i=0;i<nQueries;i++)
queries[i] = sc.nextLine();
sc.nextLine();
int result[] = new int[nQueries];
for (int i=0;i<nQueries;i++)
result[i] = 0;
for (int i=0;i<nQueries;i++){
for (int j=0;j<nStrings;j++){
if (queries[i].equals(entries[j]))
result[i]++;
else
continue;
}
}
System.out.println(Arrays.toString(result));
}
}
Can someone help me fix this problem the output is not quite correct but code seems to be right. It seems that there is some problem in comparing queries and entries because of which the the correct elements in the particular index are exchanged that is for this input
4
aba
baba
aba
xbxa
3
aba
xbxa
s
output is [1, 2, 0]
but it should've been [2,1,0]
答案1
得分: 0
你在错误的步骤使用了 sc.nextLine(); 指令... 你应该在 sc.nextInt(); 之后使用它。
在你的代码中进行了一些更改后,以下代码将通过所有测试用例:
import java.io.*;
import java.util.*;
class Solution {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int nStrings = sc.nextInt();
sc.nextLine();
String entries[] = new String[nStrings];
for (int i=0;i<nStrings;i++)
entries[i] = sc.nextLine();
int nQueries = sc.nextInt();
sc.nextLine();
String queries[] = new String[nQueries];
for (int i=0;i<nQueries;i++)
queries[i] = sc.nextLine();
int result[] = new int[nQueries];
for (int i=0;i<nQueries;i++)
result[i] = 0;
for (int i=0;i<nQueries;i++){
for (int j=0;j<nStrings;j++){
if (queries[i].equals(entries[j]))
result[i]++;
}
}
for(int i=0;i<nQueries;i++)
System.out.println(result[i]);
}
}
英文:
You are using sc.nextLine(); instruction at wrong steps... You should use it just after sc.nextInt();.
The code below will pass all the test cases... after some changes in your code.
import java.io.*;
import java.util.*;
class Solution {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int nStrings = sc.nextInt();
sc.nextLine();
String entries[] = new String[nStrings];
for (int i=0;i<nStrings;i++)
entries[i] = sc.nextLine();
int nQueries = sc.nextInt();
sc.nextLine();
String queries[] = new String[nQueries];
for (int i=0;i<nQueries;i++)
queries[i] = sc.nextLine();
int result[] = new int[nQueries];
for (int i=0;i<nQueries;i++)
result[i] = 0;
for (int i=0;i<nQueries;i++){
for (int j=0;j<nStrings;j++){
if (queries[i].equals(entries[j]))
result[i]++;
}
}
for(int i=0;i<nQueries;i++)
System.out.println(result[i]);
}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论