1031 查验身份证 (15 分)

#include <iostream>
#include <string>
using namespace std;
int main() {
    int n, i, sum = 0, cnt = 0;  // 这是为零很重要
    string s;
    int w[17] = { 7,9,10,5,8,4,2,1,6,3,7,9,10,5,8,4,2 };
    char z[11] = { '1', '0', 'X', '9', '8', '7', '6', '5', '4', '3', '2' };
    cin >> n;
    getchar();
    for (int j = 0; j < n; j++) {  // 用for,  while过不去
        sum = 0;
        getline(cin, s);
        for (i = 0; i < 17 && s[i] >= '0' && s[i] <= '9'; i++) {
            sum += (s[i] - '0') * w[i];
        }
        if (i == 17 && z[sum % 11] == s[17])
            cnt++;
        else
            cout << s << endl;
    }
    if (cnt == n)
        cout << "All passed" << endl;
    return 0;
}
原文地址:https://www.cnblogs.com/Hk456/p/10773702.html