运行结果的为什么是这样的啊?

问题:走台阶,若一次走两步则还剩一阶,一次三步剩2阶,一次五步剩4阶,一次六步剩5阶,若一次七步则刚好,求前五种可能的台阶数。

虽然可以修改代码,用稍微不同的代码可以做出来,但是我想知道,以下代码的运行结果为什么都是119

#include

int main()
{
int number_of_step=0;
int i;
for(i=0;i<5;i++)
{
for(number_of_step;;number_of_step++)
if((number_of_step%2==1)
&&(number_of_step%3==2)
&&(number_of_step%5==4)
&&(number_of_step%6==5)
&&(number_of_step%7==0))
{

printf("台阶的总数为: %d \n",number_of_step);
break;
}
}
return 0;
}

VC6.0运行的结果为:
台阶的总数为: 119
台阶的总数为: 119
台阶的总数为: 119
台阶的总数为: 119
台阶的总数为: 119
Press any key to continue

因为你固定了循环次数,当你的程序for(number_of_step;;number_of_step++)
if((number_of_step%2==1)
&&(number_of_step%3==2)
&&(number_of_step%5==4)
&&(number_of_step%6==5)
&&(number_of_step%7==0))
{

printf("台阶的总数为: %d \n",number_of_step);
break;
}
成功后就接着返回循环再次执行,所以代码的运行结果为什么都是119

int i = 0;
for(number_of_step;;number_of_step++)
if((number_of_step%2==1)
&&(number_of_step%3==2)
&&(number_of_step%5==4)
&&(number_of_step%6==5)
&&(number_of_step%7==0))
{
if(i < 5){
printf("台阶的总数为: %d \n",number_of_step);
i++;
}else{
break;
}
}

你循环输出5次结果啊,所以输出5次

建议for(number_of_step;;number_of_step++)改为r(number_of_step;;++number_of_step)或者break后++,后++是执行完循环才执行的,break后++应该不执行了.

已经知道程序的问题了,在第一次循环找到119这个数后,执行第二次循环,此时number_of_step这个变量的值还是119当然满足条件,
结果第二次循环还是输出119这个数,依次,所以五个数都是119,。我们只要在输出语句后加上number_of_step加1语句就可以啦,谢谢各位的解答了。

唔,循环的问题在于number_of_step变量的值没有改变.......此外算法可以优化一下:改为number_of_step=7开始,每次加7就行;