UVA 10327 Java运行时错误
我需要帮助解决这个名为FlipSort的UVA问题。我使用了O(nlogn)方法,使用了反转,测试用例很好。但是我不知道为什么我仍然收到UVA 10327 Java运行时错误,java,algorithm,Java,Algorithm,我需要帮助解决这个名为FlipSort的UVA问题。我使用了O(nlogn)方法,使用了反转,测试用例很好。但是我不知道为什么我仍然收到运行时错误。这是我的Java代码 import java.io.*; import java.util.*; import java.math.*; class Main { static int flips = 0; public static void main(String... args) throws IOException {
运行时错误。这是我的Java代码
import java.io.*;
import java.util.*;
import java.math.*;
class Main {
static int flips = 0;
public static void main(String... args) throws IOException {
InputStreamReader isr = new InputStreamReader(System.in);
BufferedReader in = new BufferedReader(isr);
String line;
while ((line = in.readLine()) != null) {
BigInteger[] numbers = getNumbers(in.readLine());
String ans = getAns(numbers);
System.out.println(ans);
}
}
static String getAns(BigInteger[] numbers) {
flips = 0;
mergeSort(numbers);
return "Minimum exchange operations : " + flips;
}
static BigInteger[] mergeSort(BigInteger[] numbers) {
if (numbers.length <= 1) return numbers;
int n = numbers.length;
BigInteger[] p1 = mergeSort(subArray(numbers, 0, n/2));
BigInteger[] p2 = mergeSort(subArray(numbers, n/2, n));
return merge(p1, p2);
}
static BigInteger[] merge(BigInteger[] p1, BigInteger[] p2) {
BigInteger[] ans = new BigInteger[p1.length+p2.length];
int i = 0, j = 0, k = 0;
while (i < p1.length || j < p2.length) {
if (i < p1.length && j < p2.length) {
if (p1[i].compareTo(p2[j]) <= 0) {
ans[k] = p1[i];
i++;
} else {
flips++;
ans[k] = p2[j];
j++;
}
} else if (i < p1.length) {
ans[k] = p1[i];
i++;
} else if (j < p2.length) {
ans[k] = p2[j];
j++;
}
k++;
}
return ans;
}
static BigInteger[] subArray(BigInteger[] numbers, int start, int end) {
BigInteger[] ans = new BigInteger[end-start];
for (int i = start; i < end; i++)
ans[i-start] = numbers[i];
return ans;
}
static BigInteger[] getNumbers(String line) {
String[] vals = line.split(" ");
BigInteger[] ans = new BigInteger[vals.length];
for (int i = 0; i < ans.length; i++)
ans[i] = new BigInteger(vals[i]);
return ans;
}
}
import java.io.*;
导入java.util.*;
导入java.math.*;
班长{
静态int翻转=0;
公共静态void main(字符串…参数)引发IOException{
InputStreamReader isr=新的InputStreamReader(System.in);
BufferedReader in=新的BufferedReader(isr);
弦线;
而((line=in.readLine())!=null){
BigInteger[]numbers=getNumbers(in.readLine());
字符串ans=getAns(数字);
系统输出打印LN(ans);
}
}
静态字符串getan(BigInteger[]数字){
翻转=0;
合并排序(数字);
返回“最小交换操作:”+翻转;
}
静态BigInteger[]合并排序(BigInteger[]数字){
如果(numbers.length我读取了UVA 10327。说明表明
输入将由EOF终止
我运行了你的程序,这是我键入“EOF”时的输出
3
1 2 3
Minimum exchange operations : 0
EOF
Exception in thread "main" java.lang.NumberFormatException: Zero length BigInteger
at java.math.BigInteger.<init>(BigInteger.java:296)
at java.math.BigInteger.<init>(BigInteger.java:476)
at so.UVA10327_FlipSort.getNumbers(UVA10327_FlipSort.java:73)
at so.UVA10327_FlipSort.main(UVA10327_FlipSort.java:17)
你能发布错误吗?你的运行时错误是什么?嗨!我收到了来自UVA online judge的错误,因此我无法访问更多详细信息检查你使用可能的输入进行编码,主要是边界输入。原因可能是,你为某个特定输入停止编程。尝试找出它
public static void main(String... args) throws IOException {
try {
InputStreamReader isr = new InputStreamReader(System.in);
BufferedReader in = new BufferedReader(isr);
String line;
while ((line = in.readLine()) != null) {
BigInteger[] numbers = getNumbers(in.readLine());
String ans = getAns(numbers);
System.out.println(ans);
}
} catch (Throwable t) {
// Exit gracefully...
System.exit(0);
}
}