模运算a%b返回余数,a/b但对于负数,它不会这样做.
a%b
a/b
#include <stdio.h> int main(void) { int n=-4; printf("%d\n",n%3); return 0; }
它应返回2为3*( - 2)= - 6小于-4且倍数为3但输出为-1.为什么它(-a) mod b一样对待-(a mod b)
(-a) mod b
-(a mod b)
c modulo negative-number
c ×1
modulo ×1
negative-number ×1