请各位指教输出不正常问题出在哪里,谢谢。
关于取模,我用的是VC6.0:代码:
#include<stdio.h>
int main(void)
{
printf("11/5 is %d and 11%5 is %d\n",11/5,11%5);
printf("11/-5 is %d and 11%-2 is %d\n",11/-5,11%-2);
printf("-11/-5 is %d and -11%-5 is %d\n",-11/-5,-11%-5);
printf("-11/5 is %d and -11%5 is %d\n",-11/5,-11%5);
return 0;
}
运行结果如下:
11/5 is 2 and 11 is 1
11/-5 is -2 and 11 is 1
-11/-5 is 2 and -11 is -1
-11/5 is -2 and -11 is -1
为什么不是:
11/5 is 2 and 11%5 is 1
11/-5 is -2 and 11%-2 is 1
-11/-5 is 2 and -11%-5 is -1
-11/5 is -2 and -11%5 is -1
我该了代码后:
#include<stdio.h>
int main(void)
{
printf("11/5 is %d and 11%%5 is %d\n",11/5,11%5);
printf("11/-5 is %d and 11%%-2 is %d\n",11/-5,11%-2);
printf("-11/-5 is %d and -11%%-5 is %d\n",-11/-5,-11%-5);
printf("-11/5 is %d and -11%%5 is %d\n",-11/5,-11%5);
return 0;
}
结果却变成:
11/5 is 2 and 11÷5 is 1
11/-5 is -2 and 11÷-2 is 1
-11/-5 is 2 and -11÷-5 is -1
-11/5 is -2 and -11÷5 is -1
实在搞不懂啥原因啊,请各位指教指教,先谢谢了。
已找到原因,输出显示的%太小,自己误看成÷了,实属不该啊,以后仔细点。
[ 本帖最后由 aiyameili 于 2014-3-21 22:12 编辑 ]