C++ 1031 查验身份证 (15 分)
原题
https://pintia.cn/problem-sets/994805260223102976/problems/994805290334011392
代码
#include <algorithm>
#include <iostream>
using namespace std;
const int weight[] = {7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2};
const char checkCode[] = {'1', '0', 'X', '9', '8', '7', '6', '5', '4', '3', '2'};
int main(void)
{
int n;
string IDCard;
cin >> n;
bool pass = true;//标志n个id都通过
for (int i = 0; i < n; i++)
{
cin>>IDCard;
bool flag=true;//标志前17个数都为数字
int sum=0;//注意sum=0放循环里
for (int j = 0; j < IDCard.length() - 1; j++)
{
if (IDCard[j] >= '0' && IDCard[j] <= '9')
{
sum += (IDCard[j] - '0') * weight[j];
}
else
{
cout << IDCard << endl;
flag=false;pass=false;
break;
}
}
if (flag&&IDCard[IDCard.length()-1]!= checkCode[sum % 11]){
cout << IDCard << endl;
pass=false;
}
}
if(pass) cout<<"All passed";
}

浙公网安备 33010602011771号