繁体   English   中英

意外的 output -scanf 和 function 检查

[英]unexpected output -scanf and function check

所以我正在尝试编写这个程序,但无论输入是什么,我都会得到 10。 我的 function 对我来说似乎是正确的,所以这是一个 scanf 问题吗?

编写一个程序来输入 10 个整数并确定其中有多少个整数满足以下规则: abcd = (ab + cd)2 例如 3025=(30+25) 使用接收 integer 参数的 function 如果满足上述规则,则返回 1 , 否则返回 0。

int check(int n);
int main(void)
{
    int n, i, j = 0;
    printf("Input 10 integers: ");
    for (i = 0; i < 10; i++)
    {
        scanf("%d", &n);
        if (check(n) == 1)
        {
            j++;
        }
    }
    printf("%d\n", j);
}
int check(int x)
{
    if (((x / 100) + (x % 100)) * ((x / 100) + (x % 100))) 
    {
        return 1;
    }
    else
    {
        return 0;
    }
}

我认为的问题是check function,

if (((x / 100) + (x % 100)) * ((x / 100) + (x % 100)))  // <---- anything not zero will be true
{
    return 1;
}

if中的表达式会将任何不为零的 integer 转换为真。 所写的表达式是if (x * x)如果x == 0则只有 false 。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM