意外输出 -scanf 和功能检查
unexpected output -scanf and function check
所以我正在尝试编写这个程序,但无论输入是什么,我都会得到 10。我的功能对我来说似乎是正确的,这是 scanf 问题吗?
编写一个程序,输入10个整数,判断其中有多少满足
以下规则:
abcd = (ab + cd)2 例如3025=(30+25)
使用接收整数参数returns1的函数如果满足以上
规则,returns 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
函数的问题,
if (((x / 100) + (x % 100)) * ((x / 100) + (x % 100))) // <---- anything not zero will be true
{
return 1;
}
if
中的表达式会将任何非零整数转换为真。所写的表达式是 if (x * x)
只有在 x == 0
.
时才为假
所以我正在尝试编写这个程序,但无论输入是什么,我都会得到 10。我的功能对我来说似乎是正确的,这是 scanf 问题吗?
编写一个程序,输入10个整数,判断其中有多少满足 以下规则: abcd = (ab + cd)2 例如3025=(30+25) 使用接收整数参数returns1的函数如果满足以上 规则,returns 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
函数的问题,
if (((x / 100) + (x % 100)) * ((x / 100) + (x % 100))) // <---- anything not zero will be true
{
return 1;
}
if
中的表达式会将任何非零整数转换为真。所写的表达式是 if (x * x)
只有在 x == 0
.