pat 查验身份证

分析:

分别用数组存放权重分配,校验位

判断,如果都是数字并且最后一位校验位准确,则表示通过

增加flag,便于处理全部通过情况

// pat 查验身份证.cpp : 定义控制台应用程序的入口点。
//

#include "stdafx.h"
#include<iostream>
#include<string>
using namespace std;
int a[17] = { 7,9,10,5,8,4,2,1,6,3,7,9,10,5,8,4,2 };//权重分配
int b[11] = { 1,0,10,9,8,7,6,5,4,3,2 };//校验位
string s;
bool istrue() {
	int sum = 0;
	for (int i = 0;i < 17;i++) {//17位均为数字
		if (s[i]<'0' || s[i]>'9')
			return false;
		sum += (s[i] - '0')*a[i];//加权和mod11
	}
	int temp = (s[17] == 'X') ? 10 : (s[17] - '0');//考虑特殊如X
	return b[sum % 11] == temp;

}

int main()
{
	int n, flag = 0;
	cin >> n;
	for (int i = 0;i < n;i++)
	{
		cin >> s;
		if (!istrue()) {
			cout << s << endl;
			flag = 1;// 标志位,若flag=1,表明全部通过,方便检测输出全部通过情况

		}
	}
	if (flag == 0)
		cout << "all passed";
	system("pause");
    return 0;
}

发布了32 篇原创文章 · 获赞 1 · 访问量 5392

猜你喜欢

转载自blog.csdn.net/Li060703/article/details/89216173