#include <bits/stdc++.h>
using namespace std;
int main() {
int n, allPassed = true,
weight[17]{7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2};
char M[11]{'1', '0', 'X', '9', '8', '7', '6', '5', '4', '3', '2'};
cin >> n;
for(int i = 0; i < n; i++) {
string tmp;
int skip = false;
cin >> tmp;
//判断是否全部为数字
for(int j = 0; j < 17; j++){
if(tmp[j] < '0' || '9' < tmp[j]){
allPassed = false;
skip = true;
cout << tmp << endl;
break;
}
}
if(skip) continue;
// 加权获取和
int sum = 0;
for(int j = 0; j < 17; j++){
sum += (tmp[j] - '0') * weight[j];
}
// 获取M
char m = M[sum % 11];
// 判断校验码
if(m != tmp[17]){
allPassed = false;
cout << tmp << endl;
}
}
if(allPassed) cout << "All passed";
}
求和的时候写成 = 了,应该是 +=。
string match = "10X98765432";
char M[11]{'1', '0', 'X', '9', '8', '7', '6', '5', '4', '3', '2'};
因为 char[] == string,所以校验码的数组可以改写成上面的字符串。
参考代码