英文:
Palindrome program works fine for a single test case but keeps taking input otherwise
问题
public static void main(String[] args) {
try {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int test_cases = Integer.parseInt(br.readLine());
if (test_cases >= 1 && test_cases <= 20) {
String[] ans = new String[test_cases];
for (int i = 0; i < test_cases; i++) {
char[] n = br.readLine().toCharArray();
int k = 0;
int j = n.length - 1;
while (k <= j) {
if (n[k] == n[j]) {
k++;
j--;
ans[i] = "wins";
} else
ans[i] = "loses";
}
}
for (String s : ans)
System.out.println(s);
}
} catch (Exception x) {
}
}
问题陈述:输入的第一行包含一个整数T,表示测试用例的数量。接下来的T行包含一个整数N。对于每个输入,在新的一行中输出“wins”(如果数字是回文)或“loses”(如果不是回文)。
对于test_cases=1,程序正常工作,但对于test_cases>1,程序会继续接受输入。我已经解决了回文问题,但我仍然不明白这段代码有什么问题。有人能解释一下为什么它会继续接受输入吗?
英文:
public static void main(String[] args) {
try {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int test_cases = Integer.parseInt(br.readLine());
if (test_cases >= 1 && test_cases <= 20) {
String[] ans = new String[test_cases];
for (int i = 0; i < test_cases; i++) {
char[] n = br.readLine().toCharArray();
int k = 0;
int j = n.length - 1;
while (k <= j) {
if (n[k] == n[j]) {
k++;
j--;
ans[i] = "wins";
}
else
ans[i] = "loses";
}
}
for (String s : ans)
System.out.println(s);
}
}
catch (Exception x) {
}
}
PROBLEM STATEMENT: The first line of the input contains an integer T, the number of testcases. This is followed by T lines containing an integer N. For each input output "wins" if the number is a palindrome and "loses" if not, in a new line.
For test_cases=1, the program works fine but for test_cases>1 the program keeps taking input. I have solved the palindrome problem but I still can't understand what is wrong with this code. Can anybody explain to me why does it keeps taking input?
答案1
得分: 1
For non palindrome, your code runs in infinite loop. Just add break for that.
对于非回文,您的代码会进入无限循环。只需添加break即可。
while (k <= j) {
if (n[k] == n[j]) {
k++;
j--;
ans[i] = "wins";
}
else {
ans[i] = "loses";
break;
}
}
英文:
For non palindrome, your code runs in infinite loop. Just add break for that.
while (k <= j) {
if (n[k] == n[j]) {
k++;
j--;
ans[i] = "wins";
}
else {
ans[i] = "loses";
break;
}
}
答案2
得分: 0
这可以是其中一个解决方案。
public static void main(String[] args) {
try {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int test_cases = Integer.parseInt(br.readLine());
if (test_cases >= 1 && test_cases <= 20) {
for (int i = 0; i < test_cases; i++) {
char[] n = br.readLine().toCharArray();
int k = 0;
int j = n.length - 1;
boolean flag = false;
while (k <= j) {
if (n[k] == n[j]) {
k++;
j--;
flag = true;
} else {
flag = false;
break;
}
}
if (flag) System.out.println("wins");
else System.out.println("loses");
}
}
} catch (Exception x) {
}
}
对于不是回文的字符串,你的循环会无限运行。此外,你不需要为所有测试用例存储结果在数组中,你可以每次为每个测试用例打印结果。但是,如果你打算存储字符串并稍后显示结果,可以使用类似StringBuffer的类。
英文:
This can be one of the solution.
public static void main(String[] args) {
try {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int test_cases = Integer.parseInt(br.readLine());
if (test_cases >= 1 && test_cases <= 20) {
for (int i = 0; i < test_cases; i++) {
char[] n = br.readLine().toCharArray();
int k = 0;
int j = n.length - 1;
boolean flag=false;
while (k <= j) {
if (n[k] == n[j]) {
k++;
j--;
flag=true;
}
else{
flag=false;
break;
}
}
if(flag) System.out.println("wins");
else System.out.println("loses");
}
}
}
catch (Exception x) {
}
}
For a string which is not a palindrome, your loop runs infinitely.
Also, you need not store the results in an array for all the test cases, you can print it every time for each test case. However, if you intend to store strings and display results later, you can use something like StringBuffer classes.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论