最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

java - How can I find the error in my code? I am getting an error in the following test case - Stack Overflow

programmeradmin1浏览0评论

This is the test case here, first 2 numbers are the numerator and denominator respectively of the first fraction.

The second line shows the number of queries, each line in the query has an option 1 or 2, followed by the numerator and denominator respectively of the second fraction.

Here if the option is 1 then we have to add first fraction, the second fraction, and update the answer to the first fraction. And if the option is 2 then we have multiply first fraction and the second fraction and update the answer to the first fraction.

52 71
6
2 99 6
1 86 5
2 2 51
2 27 98
2 12 75
1 11 92

Expected output should be:

858/71
10396/355
20792/18105
93564/295715
374256/7392875
115753177/680144500

But my output is coming out to be:

858/71
10396/355
20792/18105
93564/295715
374256/7392875
-383558772/441161968

Here's my code, where is the error?

import java.util.* ;
import java.io.*;

class Fraction {

    Fraction(int a, int b){
        System.out.print(a/gcd(a,b)+"/"+b/gcd(a,b));
    }

    public static int gcd(int a, int b){
        int i;
        if (a < b){
            i = a;
        }
        else{
            i = b;
        }

        for (i = i; i > 1; i--) {
            if (a % i == 0 && b % i == 0){
                return i;
            }   
        }
        return 1;
    }

}

class Solution {

    public static void main(String args[]) {

        Scanner sc = new Scanner(System.in);
        int numerator1 = sc.nextInt();
        int denominator1 = sc.nextInt();
        int query = sc.nextInt();
        
        for(int i=0;i<query;i++){
            int option = sc.nextInt();
            int numerator2 = sc.nextInt();
            int denominator2 = sc.nextInt();

            if(option == 1){
                numerator1 = (numerator1*denominator2) + (numerator2*denominator1);
                denominator1 = denominator1 * denominator2;
            }

            else if(option == 2){
                numerator1 = numerator1 * numerator2;
                denominator1 = denominator1 * denominator2;
            }

            Fraction fc = new Fraction(numerator1, denominator1);
            System.out.println();
        }
    }
}

This is the test case here, first 2 numbers are the numerator and denominator respectively of the first fraction.

The second line shows the number of queries, each line in the query has an option 1 or 2, followed by the numerator and denominator respectively of the second fraction.

Here if the option is 1 then we have to add first fraction, the second fraction, and update the answer to the first fraction. And if the option is 2 then we have multiply first fraction and the second fraction and update the answer to the first fraction.

52 71
6
2 99 6
1 86 5
2 2 51
2 27 98
2 12 75
1 11 92

Expected output should be:

858/71
10396/355
20792/18105
93564/295715
374256/7392875
115753177/680144500

But my output is coming out to be:

858/71
10396/355
20792/18105
93564/295715
374256/7392875
-383558772/441161968

Here's my code, where is the error?

import java.util.* ;
import java.io.*;

class Fraction {

    Fraction(int a, int b){
        System.out.print(a/gcd(a,b)+"/"+b/gcd(a,b));
    }

    public static int gcd(int a, int b){
        int i;
        if (a < b){
            i = a;
        }
        else{
            i = b;
        }

        for (i = i; i > 1; i--) {
            if (a % i == 0 && b % i == 0){
                return i;
            }   
        }
        return 1;
    }

}

class Solution {

    public static void main(String args[]) {

        Scanner sc = new Scanner(System.in);
        int numerator1 = sc.nextInt();
        int denominator1 = sc.nextInt();
        int query = sc.nextInt();
        
        for(int i=0;i<query;i++){
            int option = sc.nextInt();
            int numerator2 = sc.nextInt();
            int denominator2 = sc.nextInt();

            if(option == 1){
                numerator1 = (numerator1*denominator2) + (numerator2*denominator1);
                denominator1 = denominator1 * denominator2;
            }

            else if(option == 2){
                numerator1 = numerator1 * numerator2;
                denominator1 = denominator1 * denominator2;
            }

            Fraction fc = new Fraction(numerator1, denominator1);
            System.out.println();
        }
    }
}
Share Improve this question edited Feb 9 at 18:21 halfer 20.3k19 gold badges109 silver badges202 bronze badges asked Jan 28 at 10:27 IshaniIshani 111 bronze badge 4
  • 1 please edit the title to better describe your problem (the last numbers 115753177/680144500 and the negative results, suggest there is an integer overflow) – user85421 Commented Jan 28 at 10:58
  • 3 Operations with positive numbers produced a negative result? Probably an integer overflow. Use a bigger type, e.g. long or BigInteger. – teapot418 Commented Jan 28 at 11:03
  • You probably should normalize your numerator1 and denominator1 after every step of the calculation instead of only when printing them. – Thomas Kläger Commented Jan 28 at 12:32
  • 1 Poor title. Rewrite to summarize your specific technical issue. – Basil Bourque Commented Jan 28 at 17:00
Add a comment  | 

3 Answers 3

Reset to default 4

You're hitting an Integer Overflow.

If you replace your handling of ints with longs in all places, you'll get the output you expect.

To detect integer overflow, call the Math.…Exact methods such as Math.multiplyExact. Trap the ArithmeticException thrown during overflow.

Like other people already said it's probably an integer overflow.

Java uses 4 Bytes for Integers. And the number range is from -2147483648 to 2147483647.

Like other people suggest u can use long wich uses 8 Bytes. The Range is from -9223372036854775808 to 9223372036854775807.

Also in other languages like C# also exist unsigned numbers like uInt. They are only positive numbers wich gives them a range from 0 and double of the positive numbers.

Just look up primitive data types in your language.

It was showing time limit exceeded when I tried using long instead of int so I tried using BigInteger and it worked out.

Here's my final code:

import java.util.* ;
import java.io.*;
import java.math.BigInteger;

class Fraction {

    // Complete the class
    Fraction(BigInteger A, BigInteger B){
        System.out.print(A+"/"+B);
    }
}

class Solution {

    public static int gcd(int a, int b){//method to find GCD of two numbers
        int i;
        if (a < b){
            i = a;
        }
        else{
            i = b;
        }

        for (i = i; i > 1; i--) {
            if (a % i == 0 && b % i == 0){
                return i;
            }   
        }
        return 1;
    }

    public static void main(String args[]) {

        // Write your code here
        Scanner sc = new Scanner(System.in);
        int numerator1 = sc.nextInt();
        int denominator1 = sc.nextInt();
        int query = sc.nextInt();
        
        for(int i=0;i<query;i++){
            int option = sc.nextInt();
            int numerator2 = sc.nextInt();
            int denominator2 = sc.nextInt();

            if(option == 1){
                numerator1 = (numerator1*denominator2) + (numerator2*denominator1);
                denominator1 = denominator1 * denominator2;
            }

            else if(option == 2){
                numerator1 = numerator1 * numerator2;
                denominator1 = denominator1 * denominator2;
            }

            int GCD = gcd(numerator1, denominator1);//calling the method

            numerator1 = numerator1/GCD;
            denominator1 = denominator1/GCD;

            BigInteger A = BigInteger.valueOf(numerator1);//convert int to BigInteger
            BigInteger B = BigInteger.valueOf(denominator1);//convert int to BigInteger

            Fraction fc = new Fraction(A,B);
            System.out.println();
        }
    }
}
发布评论

评论列表(0)

  1. 暂无评论