好的,所以我试图用while循环创建一个程序来查找两个数字的最大公约数。这是我想出的。但是,从我所知道的情况来看,当我运行它时,程序似乎完全跳过了循环。 (操作符保持为0,除数总是等于num1)。任何人都可以帮助新手?这个while循环为什么不起作用?
/* 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;
我建议您学习如何使用调试程序逐句通过代码。 – StackedCrooked 2009-10-22 15:10:18