PAT乙级1031 查验身份证 (15 分)

https://pintia.cn/problem-sets/994805260223102976/problems/994805290334011392

#include 
using namespace std;
int main(){
	int weight[17] = {7,9,10,5,8,4,2,1,6,3,7,9,10,5,8,4,2},
		n, cnt = 0;
	char M[11] = {'1','0','X','9','8','7','6','5','4','3','2'};
	cin >> n;
	string s;
	for(int i = 0; i < n; i++){
		cin >> s;
		int sum = 0, z, flag = 0;
		for(int j = 0; j < 17 ; j++){
			if(s[j] >= '0' && s[j] <= '9')
				sum += (s[j]-'0')*weight[j];
			else
				flag = 1;
		}
		z = sum % 11;
		if(M[z] != s[17] || flag){
			cnt++;
			cout << s << endl;
		}
	}
	if(!cnt)	cout << "All passed";
	return 0;
}

你可能感兴趣的:(PAT乙级)