## PAT乙级1031 查验身份证 （Cpp）

1031 查验身份证 （15 分)

``````Z：0 1 2 3 4 5 6 7 8 9 10
M：1 0 X 9 8 7 6 5 4 3 2
``````

### 输入样例1：

``````4
320124198808240056
12010X198901011234
110108196711301866
37070419881216001X
``````

### 输出样例1：

``````12010X198901011234
110108196711301866
37070419881216001X
``````

### 输入样例2：

``````2
320124198808240056
110108196711301862
``````

### 输出样例2：

``````All passed
``````

``````#include <cstdio>
using namespace std;
int main() {
int weight[20] = {7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2};
char m[15] = {'1', '0', 'X', '9', '8', '7', '6', '5', '4', '3', '2', '1'};
char str[18];
int n, count = 0, sum, d;
scanf("%d", &n);
for (int i = 0; i < n; i++) {
d = 0;				//判断前17位是否为数字
sum = 0;			//sum为权值，每次输入需重置
scanf("%s", str);
for (int j = 0; j < 17; j++) {
if (str[j] >= '0' && str[j] <= '9') sum += weight[j] * (str[j] - 48);
else {
d = 1;
break;
}
}
sum = sum % 11;
if (str[17] == m[sum] && d != 1) count++;		//count为合法的个数
else printf("%s\n",str);
}
if (count == n) printf("All passed");
return 0;
}``````

0条评论