c++ 最大公约数 帮忙改程序 谢谢
int main()
{
int numerator,denominator,remainder,gcd;
cout<<"Please enter two positive integers:";
cin>>numerator>>denominator;
assert(denominator);
if (denominator>numerator)
{int temp=denominator;
denominator=numerator;
numerator=temp;
}
cout<<"The Greatest Common Divisor of "<<numerator<<" and "
<<denominator<<" is ";
do
{
remainder=numerator%denominator;
numerator=denominator;
denominator=remainder;
}while(remainder);
gcd=numerator;
cout<<denominator<<endl;
return EXIT_SUCCESS;
}
参考答案:do
{
remainder=numerator%denominator;
numerator=denominator;
denominator=remainder;
}while(remainder);
当最后一次remainder为0时,仍然会进入循环体内,它是先执行后判断!!
将do{}While 换成WHILE循环应该会好了!!