加法:(字符串模拟小学加法)

 string add(string s1, string s2) {
int len1 = s1.length(), len2 = s2.length();
int maxlen = max(len1, len2) + ;
string res(maxlen, '');
int flag = ;
int i = len1 - , j = len2 - , k = maxlen - ;
for (; i >= && j >= ; i--, j--) {
int temp = (s1[i] - '') + (s2[j] - '') + flag;
flag = temp / ;
res[k--]= temp % + '';
}
while (i >= ) {
int temp = s1[i--] - '' + flag;
flag = temp / ;
res[k--] = temp % + '';
}
while (j >= ) {
int temp = s2[j--] - '' + flag;
flag = temp / ;
res[k--] = temp % + '';
}
res[k] = flag + '';
if (flag) {
return res;
} else {
return res.substr();
}
}

减法:

乘法:

除法:

取模:

1)(a * b) % m = (a % m * b % m) % m

= (a % m * b) % m

= (a * b % m) % m

2)   (a + b) % m = (a % m + b % m) % m

= (a % m + b) % m

= (a + b % m) % m

举例子:

1234 % m = ((((1 * 10) + 2) * 10 + 3) * 10 + 4) % m

= (((((1 * 10) + 2) * 10 + 3) * 10) % m + 4) % m

= (((((1 * 10) + 2) * 10 + 3) % m * 10) % m + 4) % m

 long long mod(string s, int m) {
//假设字符串s符合条件,代表一个整数(正数或者负数)
int len = s.length();
int start = ;
if (s[] == '-') {
start = ;
}
long long num = ;
for (int i = start; i < len; i++) {
num = (num * + s[i] - '') % m;
}
return num;
}
05-18 12:38