简体   繁体   中英

incompatible types: BigInteger cannot be converted to int

I am trying to run this code but I am getting error which I am unable resolve. Please help.

import java.util.Scanner;
import java.math.BigInteger;
class Cseq
{
    public static void main(String []args)
    {
        Scanner jais=new Scanner(System.in);
        int x=100000;
        BigInteger i;
        BigInteger []a = new BigInteger[x];
        a[0]=BigInteger.ONE;
        for(i=BigInteger.ONE;i.compareTo(BigInteger.valueOf(x))<=0;i=i.add(BigInteger.ONE))
        {
            a[i]=a[(i.subtract(BigInteger.ONE))].multiply(i);
        }
        int t=jais.nextInt();
        while(t--!=0)
        {
        BigInteger n=jais.nextBigInteger();
        BigInteger p=jais.nextBigInteger();
        BigInteger q=jais.nextBigInteger();
        BigInteger v=(q.subtract(p)).add(BigInteger.ONE);
        BigInteger j;
        BigInteger sum=BigInteger.ZERO;
        for(j=BigInteger.ONE;j.compareTo(n)<=0;j=j.add(BigInteger.ONE))
        {
            sum=sum.add(a[v].divide(a[(v.subtract(j))].multiply(a[j])));
            sum=sum.add(v);
        }
        sum=sum.subtract(v);
        System.out.println(sum);
        }
        jais.close();
    }
}

You are getting 5 errors are all the related to array's index value. that should be integer.

In all those expression you need to change expression result in to integer value.

a[i]=a[(i.subtract(BigInteger.ONE))].multiply(i);

here i and (i.subtract(BigInteger.ONE)) should be integer.

also

sum=sum.add(a[v].divide(a[(v.subtract(j))].multiply(a[j])));

here v , (v.subtract(j)) and j should be integer.

change these index data type accordingly. Use in all these places intValue() look it here

change this line of code

a[i]=a[(i.subtract(BigInteger.ONE))].multiply(i);

to

a[i.intValue()]=a[(i.subtract(BigInteger.ONE)).intValue()].multiply(i);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM