好的,所以我正在尝试使用 while 循环创建一个程序来找到两个数字的最大公约数。这就是我想出的。但是,据我所知,该程序在我运行时似乎完全跳过了循环。(opers 保持为 0,divisor 总是返回等于 num1)。有哪位大侠可以帮助新手?
/* Define variables for divisors and number of operations */
int num1, num2, divisor, opers;
opers = 0;
/* Prompt user for integers and accept input */
cout << "Please enter two integers with the smaller number first, separated by a space. ";
cout << endl;
cin >> num1 >> num2;
/* Make divisor the smaller of the two numbers */
divisor = num1;
/* While loop to calculate greatest common divisor and number of calculations */
while ( (num1 % divisor != 0 ) && ( num2 % divisor != 0 ) )
{
divisor--;
opers++;
}
/* Output results and number of calculations performed */
cout << "The greatest common divisor of " << num1 << " and " << num2 << " is: ";
cout << divisor << endl << "Number of operations performed: " << opers;