回文程序在单个测试案例中正常工作,但在接受其他输入时会一直运行。

huangapple go评论60阅读模式
英文:

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 &gt;= 1 &amp;&amp; test_cases &lt;= 20) {
            String[] ans = new String[test_cases];
            for (int i = 0; i &lt; test_cases; i++) {
                char[] n = br.readLine().toCharArray();
                int k = 0;
                int j = n.length - 1;
                while (k &lt;= j) {
                    if (n[k] == n[j]) {
                        k++;
                        j--;
                        ans[i] = &quot;wins&quot;;
                    }
                    else
                        ans[i] = &quot;loses&quot;;
                }
            }
            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 &lt;= j) {
    if (n[k] == n[j]) {
        k++;
        j--;
        ans[i] = &quot;wins&quot;;
    }
    else {
        ans[i] = &quot;loses&quot;;
        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 &gt;= 1 &amp;&amp; test_cases &lt;= 20) {
				for (int i = 0; i &lt; test_cases; i++) {
					char[] n = br.readLine().toCharArray();
					int k = 0;
					int j = n.length - 1;
					boolean flag=false;
					while (k &lt;= j) {
						if (n[k] == n[j]) {
							k++;
							j--;
							flag=true;
						}
						else{
							flag=false;
							break;
						}
					}
					if(flag)	System.out.println(&quot;wins&quot;);
					else	System.out.println(&quot;loses&quot;);
				}
			}
		}
		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.

huangapple
  • 本文由 发表于 2020年7月31日 16:36:52
  • 转载请务必保留本文链接:https://go.coder-hub.com/63188467.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定