Java 从文本文件获取输入时,不会比较大小
我有下面的代码Java 从文本文件获取输入时,不会比较大小,java,arrays,arraylist,Java,Arrays,Arraylist,我有下面的代码 import java.io.File; import java.math.BigInteger; import java.util.ArrayList; import java.util.Arrays; import java.util.Collection; import java.util.Collections; import java.util.Scanner; public class Scalar { public static void main(Str
import java.io.File;
import java.math.BigInteger;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.Scanner;
public class Scalar {
public static void main(String args[]) throws Exception {
Scanner sc=new Scanner(new File("D:/GC/Scalar/A-small.in"));
int testcases=Integer.parseInt(sc.next());
System.out.println(testcases);
ArrayList<BigInteger> a=new ArrayList<BigInteger>();
ArrayList<BigInteger> b=new ArrayList<BigInteger>();
for(int j=0;j<2;j++){
int size=Integer.parseInt(sc.next());
System.out.println(size);
for(int i=0;i<size;i++)
{
a.add(sc.nextBigInteger());
}
for(int i=0;i<size;i++)
{
b.add(sc.nextBigInteger());
}
Collections.sort(a);
System.out.println(a.size());
System.out.println(a);
Collections.sort(b,Collections.reverseOrder());
System.out.println(b);
BigInteger sum;
for(int i=0;i<a.size();i++){
sum=a.get(i).multiply(b.get(i));
sum=sum.add(sum);
}
}
}
}
这里我尝试对第一个数组和第二个数组进行反向排序,然后进行求和和和积,这里我只取了2个案例,上面输入的1000是测试案例的总数,单个数字行表示数组大小,在我的程序中,为了确保数组的大小与给定的大小匹配,我打印了大小,在第一种情况下,输入的大小是3,我得到的是正确的,但在第二种情况下,输入的大小是5,但我得到的是8,作为数组的大小,下面是我得到的输出
1000
3
3
[-5, 1, 3]
[4, 1, -2]
5
8
[-5, 1, 1, 2, 3, 3, 4, 5]
[4, 1, 1, 1, 1, 0, 0, -2]
请告诉我哪里出了问题
谢谢这就是问题所在:
ArrayList<BigInteger> a=new ArrayList<BigInteger>();
ArrayList<BigInteger> b=new ArrayList<BigInteger>();
for(int j=0;j<2;j++){
// Stuff
}
。。。您甚至可以考虑将“Read <代码> N>代码>编号提取到一个单独的方法中,这样您的代码看起来会像:
int size = Integer.parseInt(sc.next());
System.out.println(size);
List<BigInteger> a = readBigIntegers(sc, size);
List<BigInteger> b = readBigIntegers(sc, size);
// Now sort etc.
int size=Integer.parseInt(sc.next());
系统输出打印项次(尺寸);
列表a=ReadBigInteger(sc,大小);
列表b=ReadBigInteger(sc,大小);
//现在分类等等。
也可以考虑使用<代码>扫描器.nExtTeNe()/<代码>,而不是您的显式<代码>整数。PARSETIN < /代码>调用。< /P> < P>这是问题:
ArrayList<BigInteger> a=new ArrayList<BigInteger>();
ArrayList<BigInteger> b=new ArrayList<BigInteger>();
for(int j=0;j<2;j++){
// Stuff
}
。。。您甚至可以考虑将“Read <代码> N>代码>编号提取到一个单独的方法中,这样您的代码看起来会像:
int size = Integer.parseInt(sc.next());
System.out.println(size);
List<BigInteger> a = readBigIntegers(sc, size);
List<BigInteger> b = readBigIntegers(sc, size);
// Now sort etc.
int size=Integer.parseInt(sc.next());
系统输出打印项次(尺寸);
列表a=ReadBigInteger(sc,大小);
列表b=ReadBigInteger(sc,大小);
//现在分类等等。
也可以考虑使用<代码>扫描器.nExtTeNe()/<代码>,而不是您的显式<代码>整数。PARSETIN < /代码>调用。< /P> < P>这是问题:
ArrayList<BigInteger> a=new ArrayList<BigInteger>();
ArrayList<BigInteger> b=new ArrayList<BigInteger>();
for(int j=0;j<2;j++){
// Stuff
}
。。。您甚至可以考虑将“Read <代码> N>代码>编号提取到一个单独的方法中,这样您的代码看起来会像:
int size = Integer.parseInt(sc.next());
System.out.println(size);
List<BigInteger> a = readBigIntegers(sc, size);
List<BigInteger> b = readBigIntegers(sc, size);
// Now sort etc.
int size=Integer.parseInt(sc.next());
系统输出打印项次(尺寸);
列表a=ReadBigInteger(sc,大小);
列表b=ReadBigInteger(sc,大小);
//现在分类等等。
也可以考虑使用<代码>扫描器.nExtTeNe()/<代码>,而不是您的显式<代码>整数。PARSETIN < /代码>调用。< /P> < P>这是问题:
ArrayList<BigInteger> a=new ArrayList<BigInteger>();
ArrayList<BigInteger> b=new ArrayList<BigInteger>();
for(int j=0;j<2;j++){
// Stuff
}
。。。您甚至可以考虑将“Read <代码> N>代码>编号提取到一个单独的方法中,这样您的代码看起来会像:
int size = Integer.parseInt(sc.next());
System.out.println(size);
List<BigInteger> a = readBigIntegers(sc, size);
List<BigInteger> b = readBigIntegers(sc, size);
// Now sort etc.
int size=Integer.parseInt(sc.next());
系统输出打印项次(尺寸);
列表a=ReadBigInteger(sc,大小);
列表b=ReadBigInteger(sc,大小);
//现在分类等等。
也可以考虑使用<代码>扫描器.nExtTeNe()/<代码>,而不是您的显式<代码>整数。PARSETIN < /代码>调用。< /P>