` I am doing a problem and we are supposed to write a program that reads a sequence of positive integers. The first zero terminates the input. For each input, N, the program computes the smallest base B (where B≥2) such that N is palindromic when written in base B. my code is fine for lowest palindromic bases yet when I get to a higher base with a bigger number, the .ToString method doesn't add my longs to the string correctly. It just adds random letters and numbers.
我正在做一道题,我们应该写一个程序来读取一系列正整数。第一个零结束输入。对于每个输入N,程序计算最小的基数B(其中B≥2),这样当以基数B编写时,N是回文的。我的代码对于最低的回文基数很好,但是当我得到一个更大的基数时,.ToString方法不能正确地将我的长整度加到字符串中。它只是将随机的字母和数字相加。
import java.util.Arrays;
import java.util.Scanner;
public class num5 {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
String nums[] = in.nextLine().split(" ");
for (int i = 0; i < nums.length - 1; i++) {
if (nums[i].equals("0")) {
System.exit(0);
}
System.out.println(bases(nums[i]));
}
for (String var : nums) {
System.out.println(var);
}
}
public static boolean ispalin(String original) {
String reverse = "";
int length = original.length();
for (int k = (length - 1); k >= 0; k--)
reverse = reverse + original.charAt(k);
if (original.equals(reverse))
return true;
else
return false;
}
public static int bases(String numb) {
for (int i = 2; i < Integer.MAX_VALUE; i++) {
String basecheck = "";
basecheck = Long.toString(Long.parseLong(numb), i);
if (ispalin(basecheck)) {
return i;
}
}
return 0;
}
}
Is there any way to fix this problem?
有什么办法可以解决这个问题吗?
更多回答
Please include the specific inputs and outputs you're having trouble with.
请包括您遇到问题的具体输入和输出。
What does Long.parseLong(numb)
return? Can you put a break point and inspect it?
Long.parseLong(Numb)返回什么?你能设置一个断点并检查它吗?
优秀答案推荐
To represent a number in a given base n, you must have n distinct symbols.
要以给定的n为基数表示一个数字,必须有n个不同的符号。
The range of possible bases is limited, and certainly does not go up to Integer.MAX_VALUE
. The actual limit is given in Character.MAX_RADIX
, which has the value 36
. This is what you should be using instead of Integer.MAX_VALUE
.
可能的碱基范围是有限的,并且肯定不会达到Integer.MAX_VALUE。实际限制在Character.MAX_Radix中给出,其值为36。这是您应该使用的,而不是Integer.MAX_VALUE。
public static int bases(String numb) {
for (int i = 2; i < Character.MAX_RADIX; i++) {
String basecheck = "";
basecheck = Long.toString(Long.parseLong(numb), i);
...
public static void main(String... args) {
Scanner scan = new Scanner(System.in);
String[] nums = scan.nextLine().split("\\s+");
for (String num : nums) {
if ("0".equals(num))
return;
System.out.println(base(num));
}
}
public static int base(String num) {
final BigInteger b = new BigInteger(num);
final BigInteger max = b.divide(BigInteger.TWO);
for (int i = 2; BigInteger.valueOf(i).compareTo(max) <= 0; i++)
if (isPalindrome(b.toString(i)))
return i;
return 0;
}
private static boolean isPalindrome(String str) {
for (int i = 0, j = str.length() - 1; i < j; i++, j--)
if (str.charAt(i) != str.charAt(j))
return false;
return true;
}
更多回答
我是一名优秀的程序员,十分优秀!