```c #include <stdio.h> #include <ctype.h> int validateID(char *id) { int weights[] = {7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2, 1}; int sum = 0; for (int i = 0; i < 17; i++) { if (!isdigit(id[i])) { return 0; } sum += (id[i] - '0') * weights[i]; } char last = id[17]; int lastValue; if (last == 'X' || last == 'x') { lastValue = 10; } else if (isdigit(last)) { lastValue = last - '0'; } else { return 0; } sum += lastValue * weights[17]; return (sum % 11 == 1); } int main() { int n; scanf("%d", &n); getchar(); // 读取换行符 int count = 0; char id[19]; // 18位身份证号 + '\0' for (int i = 0; i < n; i++) { fgets(id, sizeof(id), stdin); id[17] = '\0'; // 确保只读取前18个字符 if (validateID(id)) { count++; } } printf("%d\n", count); return 0; } ``` 1. 权重数组的索引与身份证号字符位置要严格对应,权重数组长度必须为18。 2. 校验最后一位字符时需要同时处理数字和'X'/'x'的情况。 3. 输入处理时要注意换行符的影响,使用getchar()或适当处理缓冲区。 [2025-05-18 21:24:01 | AI写代码神器 | 418点数解答]