PAT字符串处理题---1031 查验身份证 (15分)

1031 查验身份证 (15分)

  • 把不符合的输出
  • 没有就输出All passed
#include<iostream>
#include<ctype.h>
#include<sstream>
#include<string>
#include<cstdio>

const int maxn=110;
using namespace std;

int cnt=0;
string s[maxn];

int weight[]={7,9,10,5,8,4,2,1,6,3,7,9,10,5,8,4,2};
char m[]={'1','0','X','9', '8','7' ,'6' ,'5' ,'4' ,'3' ,'2'};
int main(){
	int n;cin>>n;
	bool ans=true;
	for(int i=0;i<n;i++){
		string a; cin>>a;
		int flag=1,sum=0;
		for(int j=0;j<a.length()-1;j++){
			if(!isdigit(a[j]))
			{
				flag=0;
				break;
			}
			else
			sum+=weight[j]*(a[j]-'0');
		}
		if(!flag||a[17]!=m[sum%=11]){
				s[cnt++]=a;
				ans=false;
		}
	}
	if(ans){
		cout<<"All passed";
	}else{
		for(int i=0;i<cnt;i++){
			cout<<s[i]<<"
";
		}
	}
	return 0;
}
原文地址:https://www.cnblogs.com/bingers/p/13083651.html