如何处理溢出错误?

时间:2022-04-29 20:20:05

I am having a code, which gives incorrect output as i am working with big numbers. I want a solution to this that how could i improve this to accommodate big numbers. Which datatype i should use?

我有一个代码,当我处理大数字时,它会给出不正确的输出。我想要一个解决这个问题的方法,我如何改进它以适应大的数字。我应该使用哪个数据类型?

CODE:

代码:

    static int get(int n,int i,int digit)
    {
      int p;
      p=(int)Math.pow(10,i-1);
      n=n/p;
      return n%10;
    }
    static boolean check_pal(int n)
    {
      int digit;
      digit=(int) (Math.log10(n)+1);
      int a=0,b=0,i,j,p;
      int sum=0;
      for(i=1,j=digit-1 ; i<=digit ; i++,j-- )
      {
        a=(int) get(n,i,digit);
        sum+=a*Math.pow(10,j);
      }
      if(sum==n)
        return true;
      else
        return false;
    }
    static int reverse(int n)
    {
        int digit;
        digit=(int) (Math.log10(n)+1);
        int a=0,b=0,i,j,p;
        int sum=0;
        for(i=1,j=digit-1 ; i<=digit ; i++,j-- )
        {
            a=(int) get(n,i,digit);
            sum+=a*Math.pow(10,j);
        }
        return n+sum;
    }
    public static void main(String[] args) {

    try{
        Scanner sc=new Scanner(System.in);
        int n=sc.nextInt();
        if(n<10 || n>999){
          System.out.println("None");   
            return;}
        boolean c;

        for(int i=1 ; i<=100 ; i++)
        {
            System.out.println("iteration"+i+" value is "+n);
        c=check_pal(n);
        if(c==true)
        {
            System.out.println(n);
            return;
        }

        n=reverse(n);
    }
    System.out.println("None");
    }

    catch(Exception e)
    {
       System.out.println("NONE"); 
    }
    }

Here is output: 如何处理溢出错误?

这是输出:

In output, Iteration 17th get negative value this shows overflow. I want a solution so that this work for all inputs between 10 to 999.

在输出中,第17次迭代得到负值,显示溢出。我想要一个解决方案,使它适用于10到999之间的所有输入。

Here is problem definition click here !!

这里是问题定义点击这里!

2 个解决方案

#1


2  

You could use long instead of int:

你可以用long代替int:

static long get(long n,long i,long digit)
    {
      long p;
      p=(long)Math.pow(10,i-1);
      n=n/p;
      return n%10;
    }

static boolean check_pal(long n)
    {
      long digit;
      digit=(long) (Math.log10(n)+1);
      long a=0,b=0,i,j,p;
      long sum=0;
      for(i=1,j=digit-1 ; i<=digit ; i++,j-- )
      {
        a=(long) get(n,i,digit);
        sum+=a*Math.pow(10,j);
      }
      if(sum==n)
        return true;
      else
        return false;
    }

static long reverse(long n)
    {
        long digit;
        digit=(long) (Math.log10(n)+1);
        long a=0,b=0,i,j,p;
        long sum=0;
        for(i=1,j=digit-1 ; i<=digit ; i++,j-- )
        {
            a=(long) get(n,i,digit);
            sum+=a*Math.pow(10,j);
        }
        return n+sum;
    }

iteration25 value is 8813200023188

iteration25值是8813200023188

By the way: your check_pal method could be much shorter:

顺便说一句:check_pal方法可以短得多:

 static boolean check_pal(long n){
    return reverse(n) == n;
 }

 static long reverse(long n)
    {
        long digit;
        digit=(long) (Math.log10(n)+1);
        long a=0,b=0,i,j,p;
        long sum=0;
        for(i=1,j=digit-1 ; i<=digit ; i++,j-- )
        {
            a=(long) get(n,i,digit);
            sum+=a*Math.pow(10,j);
        }
        return sum;
    }

 static long reverseAndAdd(long n){
     return n + reverse(n);
 }

(note I changed the last line of the reverse method and added a reverseAndAdd, because your reverse does not do what it says :-))

(注意,我更改了逆向方法的最后一行,并添加了一个reverseAndAdd,因为您的逆向操作不按它说的做:-)

#2


5  

You can use java.math.BigInteger class:

您可以使用java.math。BigInteger类:

http://docs.oracle.com/javase/7/docs/api/java/math/BigInteger.html

http://docs.oracle.com/javase/7/docs/api/java/math/BigInteger.html

#1


2  

You could use long instead of int:

你可以用long代替int:

static long get(long n,long i,long digit)
    {
      long p;
      p=(long)Math.pow(10,i-1);
      n=n/p;
      return n%10;
    }

static boolean check_pal(long n)
    {
      long digit;
      digit=(long) (Math.log10(n)+1);
      long a=0,b=0,i,j,p;
      long sum=0;
      for(i=1,j=digit-1 ; i<=digit ; i++,j-- )
      {
        a=(long) get(n,i,digit);
        sum+=a*Math.pow(10,j);
      }
      if(sum==n)
        return true;
      else
        return false;
    }

static long reverse(long n)
    {
        long digit;
        digit=(long) (Math.log10(n)+1);
        long a=0,b=0,i,j,p;
        long sum=0;
        for(i=1,j=digit-1 ; i<=digit ; i++,j-- )
        {
            a=(long) get(n,i,digit);
            sum+=a*Math.pow(10,j);
        }
        return n+sum;
    }

iteration25 value is 8813200023188

iteration25值是8813200023188

By the way: your check_pal method could be much shorter:

顺便说一句:check_pal方法可以短得多:

 static boolean check_pal(long n){
    return reverse(n) == n;
 }

 static long reverse(long n)
    {
        long digit;
        digit=(long) (Math.log10(n)+1);
        long a=0,b=0,i,j,p;
        long sum=0;
        for(i=1,j=digit-1 ; i<=digit ; i++,j-- )
        {
            a=(long) get(n,i,digit);
            sum+=a*Math.pow(10,j);
        }
        return sum;
    }

 static long reverseAndAdd(long n){
     return n + reverse(n);
 }

(note I changed the last line of the reverse method and added a reverseAndAdd, because your reverse does not do what it says :-))

(注意,我更改了逆向方法的最后一行,并添加了一个reverseAndAdd,因为您的逆向操作不按它说的做:-)

#2


5  

You can use java.math.BigInteger class:

您可以使用java.math。BigInteger类:

http://docs.oracle.com/javase/7/docs/api/java/math/BigInteger.html

http://docs.oracle.com/javase/7/docs/api/java/math/BigInteger.html