2个C++之间的最高CF

2个C++之间的最高CF,c++,C++,这是一个寻找最高公因数的程序: #include <iostream> enter code here using namespace std; void main () { int max=0 , min=0 , x,y,hcf=0; cout<<"enter 2 numbers\n"; cin>>x>>y; if (x>y) { max=x; min=y; } else if (y>x) { ma

这是一个寻找最高公因数的程序:

#include <iostream>
enter code here using namespace std;
void main ()
{
int max=0 , min=0 , x,y,hcf=0;
cout<<"enter 2 numbers\n";
cin>>x>>y;
if (x>y)
{   
    max=x;
    min=y;
}
else if (y>x)
{   
    max=y;
    min=x;
}
if
 (max%min==0)
{ hcf=min;
cout<<"highest cf is:"<<hcf<<endl;}
else
    {
        hcf=min;
do
{
    hcf--;
}
while (max%hcf!=0 && min%hcf!=0);

cout<<"highest cf is:"<<hcf<<endl;
}
        system ("pause");
}
虽然当我测试它时;它采用的条件为最大%hcf=0并忽略第二个。。。例如,如果我输入12和8,那么12被称为max,8被称为min,那么它输出6作为hcf 这意味着它忽略了第二个条件。。。我当然明白。
那么我的错误在哪里呢?

获取HCF的一个简单解决方案就是这样做

int HCF(int num1, int num2)
{
    for(int i = num1; i > 2; i++)
    {
        if(num1 % i == 0 && num2 % i == 0) return i;
    }
    return 1;
}

这正是您应该学习如何使用调试器的程序。查找短路。如果最大%hcf=0为false,整个条件无论如何都将为false-因此无需计算第二项。当其中一个条件为true时,您必须留在循环中。循环中的测试是错误的。