You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

23 lines
590 B

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

#include <bits/stdc++.h>
using namespace std;
/**
功能:正数+负数取模
n=3为例
MOD就是一直0,1,2,0,1,2这样的向下走不会乱了规矩
MOD 0 1 2 0 1 2 0 1 2
@ @ @ @ @ @ @ @ @
a -3 -2 -1 0 1 2 3 4 5
*/
int MOD(int a, int b) {
return (a % b + b) % b; //配合数组下标从0开始合适
}
int main() {
int n = 5;
for (int a = -4; a <= 2; a++) {
cout << "a=" << a << " MOD=" << MOD(a, n) << endl;
}
return 0;
}