当x变成40时,y应该是0吧?为什么当 i > 1 为 false 时,内部 for 循环仍然运行?

问题描述 投票:0回答:1
#include <stdio.h>
int main()
{
    int num = 40585;
    int output = 0;

    for (int x = num; x > 0; x /=10)
    {
        int temp = 1;
        int y = x % 10;

        for (int i = y; i > 1; i--)
            temp *= i;

        output += temp;
    }

    if (output == num)
        printf("True");
    else
        printf("False");
} 

我以为当 i 变成 0 时,内部 for 循环中的语句将不会运行,因此输出将为 0。当全部添加时,我认为结果是 40584。

c factorial
1个回答
0
投票

x
为40时,
y
设置为0,并且不执行
i
上的循环迭代。

这使得

temp
保持不变。
temp
被初始化为1,所以它仍然是1,并且
output += temp
将1加到
output

© www.soinside.com 2019 - 2024. All rights reserved.