简体   繁体   English

查找 GCD 和 LCM 为某些测试用例提供正确的 output 但我的提交显示错误答案

[英]Finding GCD and LCM giving correct output for some test cases but my submission shows wrong answer

My c++ code of finding gcd and lcm is giving correct output for some test cases but my submission shows wrong answer.我的 c++ 查找 gcd 和 lcm 的代码为某些测试用例提供了正确的 output 但我提交的答案显示错误。

int T;
cin>>T;
int x,y,a,b;

while(T--)
{
    cin>>x>>y;
    a=(x>y)?x:y;  // a will be the greater number
    b=(y>x)?x:y;  // b will be the smaller number
    int product=a*b;
    
    ///we will find gcd, and lcm will be a*b/gcd
    
    if(a%b==0)
        cout<<b<<" "<<a<<endl;   // the smaller one will be gcd and the greater one, lcm
    else
    {
        int rem=a%b;
        
        while(rem!=0)
        {
            a=b;
            b=rem;
            rem=a%b;
        }
        cout<<b<<" "<<product/b<<endl;
    }
}

Are there certain test cases I am missing?我缺少某些测试用例吗? Or maybe my code is wrong.或者也许我的代码是错误的。

There are few cases on which it might fail:在少数情况下它可能会失败:

  1. When numbers x and y are not able to fit in int data type.当数字xy不适合int数据类型时。
  2. What if either x or y = 0?如果xy = 0 会怎样?
  3. You are doing product = a*b .你正在做product = a*b This also can lead to overflow resulting wrong output in else part.这也可能导致溢出导致错误的 output 在else部分。

The correct code is as follows:正确的代码如下:

` `

int T;
cin>>T;
long int x,y,a,b;

while(T--)
{
    
    cin>>x>>y;
    a=(x>y)?x:y;  // a will be the greater number
    b=(y>x)?x:y;  // b will be the smaller number
    long int product=a*b;
    
    ///we will find gcd, and lcm will be a*b/gcd
    
    if(a==0 && b==0)
    {
        cout<<"0 0"<<endl;
    }
    
     else if(b==0)
    {
        cout<<a<<" 0"<<endl;
    }
    
    
    else
    {
        
    
    if(a%b==0)
    cout<<b<<" "<<a<<endl;   // the smaller one will be gcd and the greater one, lcm
    
    else
    {
        int rem=b;
        
        while(a%b!=0)
        {
            rem=a%b;
            a=b;
            b=rem;
        
            
            
        }
        cout<<rem<<" "<<product/b<<endl;
        
    }
    
    }
    
    
}

` `

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我的代码通过了测试用例,但是当我提交它时显示错误的答案 - My code passes the test cases but when I submit it shows wrong answer 193.在A2Oj上总是显示Wrong Answer to my submit? - 193. on A2Oj always shows Wrong Answer to my submission? 一系列墙体的雨水收集问题以及在一些错误答案 130/150 通过的大型测试用例上面临的问题 - Rainwater harvesting problem for a series of walls and facing an issue on some large test cases of wrong answer 130/150 passed 在多个测试用例中出现意外输出。我的解决方案出了什么问题? - Unexpected output in multiple test cases.What is wrong with my solution? 从树中删除边缘的解决方案为某些测试用例提供了错误的答案 - Solution for Delete edges from a tree is giving wrong answers for some test cases 输出是正确的,但 SPOJ 给了我错误的答案 - Output is correct but SPOJ gave me wrong answer 我正在尝试从网站获取此 output 但它显示错误的 output 但我的回答是正确的 - I am trying to get this output but its showing wrong output from a website but my answer is correct 为什么我的代码在SPOJ上给出错误的答案? - Why my code is giving wrong answer on SPOJ? 请找出我的代码中的错误,它仍然适用于我的所有测试用例,同时提交得到错误的答案 - Please find out the error in my code it is working for all my test cases still while submitting getting the wrong answer Sherlock 和 Array hackerrank 问题在测试用例上的“错误答案” - Sherlock and Array hackerrank problem 'wrong answer' on test cases
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM