首页 > 试题广场 >

1031. 查验身份证(15)

[编程题]1031. 查验身份证(15)
  • 热度指数:17572 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 32M,其他语言64M
  • 算法知识视频讲解
一个合法的身份证号码由17位地区、日期编号和顺序编号加1位校验码组成。校验码的计算规则如下:
首先对前17位数字加权求和,权重分配为:{7,9,10,5,8,4,2,1,6,3,7,9,10,5,8,4,2};然后将计算的和对11取模得
到值Z;最后按照以下关系对应Z值与校验码M的值:
Z:0 1 2 3 4 5 6 7 8 9 10
M:1 0 X 9 8 7 6 5 4 3 2
现在给定一些身份证号码,请你验证校验码的有效性,并输出有问题的号码。

输入描述:
输入第一行给出正整数N(<= 100)是输入的身份证号码的个数。随后N行,每行给出1个18位身份证号码。


输出描述:
按照输入的顺序每行输出1个有问题的身份证号码。这里并不检验前17位是否合理,只检查前17位是否全为数字且最后1位校验码计算准确。如果所有号码都正常,
则输出“All passed”。
示例1

输入

4
320124198808240056
12010X198901011234
110108196711301866
37070419881216001X

输出

12010X198901011234
110108196711301866
37070419881216001X
#ifndef Basic_Level_1021_CPP
#define Basic_Level_1021_CPP

#include<iostream>
#include<string>

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};
    char M[11]={'1','0','x','9','8','7','6','5','4','3','2'};
    int N;
    cin>>N;
    string A;

    int flag=1;//whether the data is all passed
    for(int i=0;i<N;++i)
    {
        int sum=0;
        int is_wrong=0;
        cin>>A;
        for(int j=0;j<17;++j)
        {
            if(A[j]<'0'||A[j]>'9')
            {
                cout<<A<<endl;
                flag=0;
                is_wrong=1;
                break;
            }
            sum+=(A[j]-'0')*weight[j];
        }
        //judge the last char
        int z=sum%11;
        char right=M[z];
        if(A[17]!=right&&is_wrong==0)
        {
            flag=0;
            cout<<A<<endl;
        }
    }
    if(flag)
    {
        cout<<"All passed"<<endl;
    }
    return 0;
}

#endif // Basic_Level_1021_CPP


发表于 2015-10-24 13:24:23 回复(1)
import java.util.*;

/**
 * 查验身份证
 * 题目描述
 * 一个合法的身份证号码由17位地区、日期编号和顺序编号加1位校验码组成。校验码的计算规则如下:
 * 首先对前17位数字加权求和,权重分配为:{7,9,10,5,8,4,2,1,6,3,7,9,10,5,8,4,2};
 * 然后将计算的和对11取模得到值Z;最后按照以下关系对应Z值与校验码M的值:
 * Z:0 1 2 3 4 5 6 7 8 9 10
 * M:1 0 X 9 8 7 6 5 4 3 2
 * 现在给定一些身份证号码,请你验证校验码的有效性,并输出有问题的号码。
 * 输入描述:
 * 输入第一行给出正整数N(<= 100)是输入的身份证号码的个数。随后N行,每行给出1个18位身份证号码。
 * 输出描述:
 * 按照输入的顺序每行输出1个有问题的身份证号码。这里并不检验前17位是否合理,只检查前17位是否全为
 * 数字且最后1位校验码计算准确。如果所有号码都正常,
 * 则输出“All passed”。
 * 输入例子:
 * 4
 * 320124198808240056
 * 12010X198901011234
 * 110108196711301866
 * 37070419881216001X
 * 输出例子:
 * 12010X198901011234
 * 110108196711301866
 * 37070419881216001X
 *
 * @author shijiacheng
 * @date 2018/1/29
 */
public class B1021CheckIDCard {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int[] weight = {7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2};
        Map<Integer, String> vertify = new HashMap<>();
        for (int i = 0; i < 11; i++) {
            if (i < 2) {
                vertify.put(i, String.valueOf(1 - i));
            } else if (i == 2) {
                vertify.put(i, "X");
            } else {
                vertify.put(i, String.valueOf(12 - i));
            }
        }
        int N = sc.nextInt();
        List<String> list = new ArrayList<>();
        for (int i = 0; i < N; i++) {
            list.add(sc.next());
        }
        List<String> errorList = new ArrayList<>();
        for (int i = 0; i < list.size(); i++) {
            if (list.get(i).length() == 18) {
                if (list.get(i).contains("X")) {
                    if (list.get(i).charAt(17) == 'X') {
                        if (!vertify(list.get(i), weight, vertify)) {
                            errorList.add(list.get(i));
                        }
                    } else {
                        errorList.add(list.get(i));
                    }
                } else {
                    if (!vertify(list.get(i), weight, vertify)) {
                        errorList.add(list.get(i));
                    }
                }
            } else {
                errorList.add(list.get(i));
            }
        }

        if (errorList.size() == 0) {
            System.out.println("All passed");
        } else {
            for (int i = 0; i < errorList.size(); i++) {
                System.out.println(errorList.get(i));
            }
        }

    }

    public static boolean vertify(String id, int[] weight, Map<Integer, String> vertify) {

        int sum = 0;
        for (int i = 0; i < 17; i++) {
            int temp = id.charAt(i) - '0';
            sum += temp * weight[i];
        }

        int result = sum % 11;
        if (vertify.get(result).equals(String.valueOf(id.charAt(17)))) {
            return true;
        } else {
            return false;
        }
    }
}
发表于 2018-01-29 19:29:28 回复(0)
字符转int,可以减去 '0'。char本质就是阿斯克码,int数字。
#include<iostream>
using namespace std;
int w[17]={7,9,10,5,8,4,2,1,6,3,7,9,10,5,8,4,2};
int M[11]={1,0,'X'-'0',9,8,7,6,5,4,3,2};
bool valid(string s){
	int Z,sum=0;
	for(int i=0;i<17;i++){
		if(s[i]<'0'||s[i]>'9')return false;
		sum+=(s[i]-'0')*w[i];
	}
	Z=sum%11;
	return M[Z]==s[17]-'0';
} 
int main() {
	bool allpass=true;
	int N;
	string s;
	cin>>N;       
	while(N--){
		cin>>s;
		if(!valid(s)){
			cout<<s<<endl;
			allpass=false;
		}
	}
	if(allpass)cout<<"All passed";
    return 0;
}


发表于 2020-02-01 09:12:42 回复(1)
#include <cstdio>
#include <cstring>
#include <string>
#include <cstdlib>
#include <iostream>
#include <cmath>
#include <algorithm>
using namespace std;//思路:用数组将权值保存,将存入的身份证号与权值一一对应,再求余即可
int a[]= {7,9,10,5,8,4,2,1,6,3,7,9,10,5,8,4,2};
char s[20],c[]= {"10X98765432"};
int main()
{
    int n,len1,i,num=0;
    scanf("%d",&n);
    int m=n;
    while(n--)
    {
        int sum=0;
           scanf("%s",s);
        len1=strlen(s);
        for(i=0; i<len1-1; i++)
        {
            if('0'>s[i]||s[i]>'9')
            {
                sum=-1;
                break;
            }
            sum+=(s[i]-'0')*a[i];
        }
        sum=sum%11;
        if(s[len1-1]==c[sum])//每次如果验证码正确就加1
            num++;
        else
            sum=-1;
        if(sum==-1)
           cout<<s<<endl;
    }
    if(num==m)
        printf("All passed");
    return 0;
} 

发表于 2017-07-29 11:01:16 回复(4)
import java.util.Scanner;
public class Main {
	private static final char[] M = {'1','0','X','9','8','7','6','5','4','3','2'};
	private static final int[] W = {7,9,10,5,8,4,2,1,6,3,7,9,10,5,8,4,2};
	public static void main(String[] args) {
		Scanner in = new Scanner(System.in);
		int n = in.nextInt();
		String[] errors = new String[n];
		int count = 0;
		for(int i = 0;i<n;i++){
			String id = in.next();
			if(!check(id))
				errors[count++] = id;
		}
		if(count==0)
			System.out.println("All passed");
		else
			for(int i = 0;i<count;i++)
				System.out.println(errors[i]);
		
	}
	private static boolean check(String id){
		if(id.length()!=18)
			return false;
		int w = 0;
		for(int i = 0;i<17;i++){
			char c = id.charAt(i);
			if(c>='0'&&c<='9')
				w += W[i]*(c-'0');
			else
				return false;
		}
		return M[w%11]==id.charAt(17);
	}
}

发表于 2016-06-11 16:32:59 回复(0)
#include<bits/stdc++.h>  
using namespace std;
int main(){
    string check = "10X98765432";
    string idNumber;
    int weight[17] = {7,9,10,5,8,4,2,1,6,3,7,9,10,5,8,4,2};
    int sign , sum , n ;
    sign = 0;
    cin>>n;
    while(n--){
        cin>>idNumber;
        sum = 0;
        for(int i = 0; i < 17; i ++){
            if(idNumber[i]<'0' | idNumber[i]>'9')
            {
                // 检查是否有非数字
                cout<<idNumber<<endl;
                sign = 1;
                break;
            }
            sum += (idNumber[i] - '0') * weight[i];          
        }
        if(check[sum % 11] != idNumber[17]){
            // 计算加权求和值
            cout<<idNumber<<endl;
            sign = 1;
        }
    }
    if(!sign){
        cout<<"All passed";
    }
    return 0;
}

发表于 2020-10-27 15:07:38 回复(0)
import java.util.*;
public class Main{
    public static void main(String []args){
       Scanner in=new Scanner(System.in);
        int N=in.nextInt();
        char c[][]=new char[N][18];
        String s[]=new String[N];
        int n=0;
        for(int i=0;i<N;i++) {
            s[i]=in.next();
        }
        for(int i=0;i<N;i++) {
            for(int j=0;j<18;j++) {
                c[i][j]=s[i].toCharArray()[j];
            }
        }
        char CheckCode[]={'1', '0', 'X', '9','8', '7', '6', '5', '4', '3', '2'};
        int w[]={7,9,10,5,8,4,2,1,6,3,7,9,10,5,8,4,2};
        for(int i=0;i<N;i++){
             int count=0,sum=0;
            for(int j=0;j<17;j++){
                 if(!Character.isDigit(c[i][j])) {
                     for(int k=0;k<18;k++){
                         System.out.print(c[i][k]); 
                     }
                 System.out.println();
                 n++;
                 }
                else
                {
                    count++;
                    if(count==17){
                        for(int jj=0;jj<17;jj++)
                        sum+=(c[i][jj]-'0')*w[jj];
                       if(CheckCode[sum%11]!=c[i][17]) {
                           for(int k=0;k<18;k++){
                         System.out.print(c[i][k]); 
                           }
                           System.out.println();
                           n++;
                       }      
                    }
                }  
            }
        }
        if(n==0) System.out.println("All passed");
    }
}

发表于 2019-04-29 21:15:12 回复(0)

又来分享Python版了

while True:
    try:
        num = int(input())
        idLists = []
        checkCode = "10X98765432"
        weights = [7,9,10,5,8,4,2,1,6,3,7,9,10,5,8,4,2]
        for i in range(num):
            idLists.append(input())
        wrongResult = []
        for i in range(num):
            codeSum = 0
            if idLists[i][:17].isdigit():  #检查前十七位是否为数字
                for j in range(17):       #循环加权重
                    codeSum += weights[j]*int(idLists[i][j])
                if checkCode[codeSum%11] != idLists[i][17]:
                    wrongResult.append(idLists[i])
            else:
                wrongResult.append(idLists[i])
        if len(wrongResult) == 0:
            print("All passed")
        else:
            for i in wrongResult:
                print(i)
    except Exception:
        break
编辑于 2018-09-22 10:36:26 回复(0)

#include <bits/stdc++.h> using namespace std; int main(){     int m;     string no;     long long int i,t=0,number,sum;     int a[12]={1,0,'X',9,8,7,6,5,4,3,2};     int b[19]={7,9,10,5,8,4,2,1,6,3,7,9,10,5,8,4,2};     cin >> m;     number=m;     while(m--){         cin >> no;         i=0;         sum=0;         while(no[i] != '\0'){             if(no[i] >= '0' && no[i] <= '9'){                 if(i < 17)                     sum+=(no[i] - '0')*b[i];             }             else if(i != 17 ){cout << no << endl;break;}                 if(i == 17){                       //  cout << sum <<endl;                     sum%=11;                    // cout << sum << ' ' << t << endl;                     if(a[sum] == (no[i]-'0') || sum == 2 && no[i] == 'X'){                         t++;                     }                     if(a[sum] != no[i]-'0')                     {                         if(no[i] == 'X')                         {                             if(sum != 2)                                 cout << no << endl;                         }                             else cout << no << endl;                         }                 }                 i++;         }     }     if(t == number)cout << "All passed" << endl;     return 0; }


编辑于 2018-09-12 14:45:37 回复(0)
//小白第一步,终于成功了,激动到想哭~.~
import java.util.*; public class Main{     private static final int[] W={7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2};     private static final char[] M = {'1','0','X','9','8','7','6','5','4','3','2'};     public static void main(String[] args){         Scanner sc = new Scanner(System.in);                  int N=sc.nextInt();         int count=0;         ArrayList<String> errors=new ArrayList<String>();         for(int i=0;i<N;i++){             String id=sc.next();             if (!checkId(id)){                 count+=1;                 errors.add(id);             }         }         if(count != 0){             for(int i=0;i<errors.size();i++){                 System.out.println(errors.get(i));             }         }         else{             System.out.print("All passed");         }     }     public static boolean checkId(String id){         int sum=0;         for (int j=0;j<17;j++){             if((id.charAt(j)>='0') && (id.charAt(j)<='9')){                 sum+=W[j]*(id.charAt(j)-'0');             }             else{                 return false;             }         }         return M[sum%11]==id.charAt(17);     } }
# coding=utf-8
#小白的Python版本,开心>.<
W=[7,9,10,5,8,4,2,1,6,3,7,9,10,5,8,4,2]
M=['1','0','x','9','8','7','6','5','4','3','2']
errors=[]
n=int(raw_input())
def Main(n,W,M):
for i in range(n):
id =raw_input()
if not checkId(id,W,M):
errors.append(id)
if len(errors)==0 :
print "All passed"
else:
for i in errors:
print i
def checkId(id,W,M):
sum=0
id_part=id[:17]
for i,c in enumerate(id_part):
if (c>='0') and (c <='9'):
#print c
sum+=W[i]*int(id_part[i])
else: 
return False
return M[sum%11]==id[-1]
Main(n,W,M)

编辑于 2018-05-09 15:07:16 回复(0)
import java.util.*;

public class IdReaserch {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int N = sc.nextInt();
        List<String> lt = new ArrayList<String>();
        int sum=0;
        int[] bit = new int[] {7,9,10,5,8,4,2,1,6,3,7,9,10,5,8,4,2};
        char[] M = new char[] {1,0,'X',9,8,7,6,5,4,3,2};
        
        for(int i = 0; i<N; i++) {
            String s = sc.next();
            for(int j = 0;j<17;j++) {
                if(s.charAt(j)=='X') {
                    lt.add(s);
                    sum = 0;
                    break;
                }else {
                    int x = (int)s.charAt(j)-'0';
                    sum +=(x*bit[j]);
                    if(j==16) {
                        if(M[sum%11]==s.charAt(17)-'0') {
                            sum= 0;
                            break;
                        }else
                    
                            lt.add(s);
                            sum = 0;
                            break;
                    }
                }
            }
        }
        
        if(lt.size() == 0) {
            System.out.println("All passed");
        }else {
            for(int z = 0;z<lt.size();z++) {
                System.out.println(lt.get(z));
            }
        }
    }
}

发表于 2018-04-02 21:33:31 回复(0)
#include <iostream>
#include <string.h>
#include <ctype.h>

using namespace std;

class Str
{
public:
    char chs[18];
    int flag;

    void SetStr(char * ch){
            memcpy(chs,ch,18);
            flag = 1;
    }

    void isnum()
    {
        int i;
        for(i=0;i<18;i++){
            if(!(isdigit(chs[i]))) {
                flag = 0;
                break;
            }
        }
    }

    void checkout()
    {
        int result=0,i;
        char rst;
        int a[17] = {7,9,10,5,8,4,2,1,6,3,7,9,10,5,8,4,2};
        char corr[11] = {'1','0','X','9','8','7','6','5','4','3','2'};
        for(i=0;i<17;i++){
            result += (chs[i]-'0')*a[i];
        }
        result = result % 11;
        rst = corr[result];
        if (rst != chs[17])
            this->flag=0;
    }
};

int main()
{
    int n,i,count=0;
    cin >> n;
    Str strings[n];
    for(i=0;i<n;i++){
        char temp[18];
        cin >> temp;
        strings[i].SetStr(temp);
        strings[i].isnum();

        if(strings[i].flag==1)
            strings[i].checkout();

        if(strings[i].flag==1)
            count++;
        else
            cout << temp <<endl;
    }
    if(count == n)
        cout << "All passed" <<endl;
    return 0;
}
编辑于 2017-07-06 10:16:14 回复(0)
/*
    解题思路:分割字符串进行比较
*/
#include <iostream>
#include <cctype>
#include <string>
#include <vector>
#include <cstdlib>
#include <algorithm>
using namespace std;
int quan[17]={7,9,10,5,8,4,2,1,6,3,7,9,10,5,8,4,2};
char mo[11]={'1','0','x','9','8','7','6','5','4','3','2'};
bool isdigit_str(const string str){
    int count=0;
    for(int i=0;i<str.length();i++){
        if(isdigit(str[i])) count++;
    }
    if(count==str.length())return true;
    else return false;
}
char qu_mo(const string str){
    int sum=0;
    for(int i=0;i<str.length();i++){
        int temp = str[i]-'0';
        sum+=temp*quan[i];
    }
    sum=sum%11;
    return mo[sum];
}
int main()
{
    int N;
    int count=0;
    cin>>N;
    vector<string> res;
    for(int i=0;i<N;i++){
        string str;
        cin>>str;
        string str2=str.substr(0,17);
        if(isdigit_str(str2)){
            if(qu_mo(str2)==str[17]) count++;
            else res.push_back(str);
        }else res.push_back(str);
    }
    if(count==N)cout<<"All passed"<<endl;
    else for(vector<string>::iterator it=res.begin();it!=res.end();it++){
            cout<<*it<<endl;
    }
    return 0;
}

发表于 2017-04-24 20:30:59 回复(0)
import java.util.*;
public class Main {
	static int[] weight = {7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2};
	static char[] M = {'1', '0', 'X', '9', '8', '7', '6', '5', '4', '3', '2'};
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		while (sc.hasNext()) {
			int n = sc.nextInt();
			String[] arr = new String[n];
			for (int i = 0; i < n; i ++ )
				arr[i] = sc.next();
			boolean isFinded = false;
			for (int i = 0; i < n; i ++ ) {
				String s = arr[i];
				if(s.substring(0, 17).contains("X")) {
					isFinded = true;
					System.out.println(s);
				} else {
					int sum = 0;
					for (int j = 0; j < 17; j ++ )
						sum += (s.charAt(j) - '0') * weight[j];
					if(s.charAt(17) != M[sum % 11]) {
						isFinded = true;
						System.out.println(s);
					}
				}
			}
			if( ! isFinded) System.out.println("All passed");
		}
	}
}

发表于 2016-10-20 15:49:01 回复(0)
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace Question21ID
{
    class Program     {
        static void Main(string[] args)
        {
            var N = int.Parse(Console.ReadLine());
            var ids = Enumerable.Range(0, N).Select(s => Console.ReadLine()).ToArray();
 
            var right = new[] { 7910584216379105842 };
 
            var mV = @"10X98765432";
            var index = Enumerable.Range(017);
            var errorcount = ids.Where(item =>             {
                return mV[index.Select(s => right[s] * (item[s] - '0')).Sum() % 11]!=item.Skip(17).First();
            });
            if (errorcount.Any())
            {
                foreach (var item in errorcount)
                {
                    Console.WriteLine(item);
                }
            }
            else             {
                Console.WriteLine("All passed");
            }
        }
    }
}

发表于 2016-03-03 17:18:49 回复(0)
啥头像
总体思路:
    检查身份证有效性

代码如下:
#include <iostream>

using namespace std;

int weight[17] = {7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2};
char M[11] = {'1', '0', 'X', '9', '8', '7', '6', '5', '4', '3', '2'};

bool isValid(string id);

int main()
{
    ios::sync_with_stdio(false);
    int N; cin >> N;
    int validNum = 0;
    for(int i=0; i<N; i++) {
        string id; cin >> id;
        if(isValid(id)) {
            validNum++;
        } else {
            cout << id << endl;
        }
    }
    if(N == validNum) {
        cout << "All passed" << endl;
    }
    return 0;
}

bool isValid(string id)
{
    int sum = 0;
    for(int i=0; i<17; i++) {
       if(id[i]>='0' && id[i]<='9') {
        sum += (int)(id[i]-'0')*weight[i];
       } else {
        return false;
       }
    }
    sum %= 11;
    if(M[sum] == id[17]) {
        return true;
    } else {
        return false;
    }
} 


发表于 2015-10-21 14:00:30 回复(0)
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

public class Main {
	static final int[] arrQz={7,9,10,5,8,4,2,1,6,3,7,9,10,5,8,4,2};
	static final char[] arrM={'1','0','X','9','8','7','6','5','4','3','2'};
	public static void main(String[] args) {
		// TODO 自动生成的方法存根
		Scanner sc=new Scanner(System.in);
		int N=sc.nextInt();
		List lStr=new ArrayList<String>();
		for(int i=0;i<N;i++){
			String str=sc.next();
			if(!isHf(str)){
				lStr.add(str);
			}
		}
		//System.out.println(lStr.get(0));
		if(lStr.isEmpty()){
			System.out.print("All passed");
		}else{
			for(int i=0;i<lStr.size();i++){
				System.out.println(lStr.get(i));
			}
		}
	}
	public static boolean isHf(String str){
		//int num=Integer.parseInt(str);
		boolean res = true;
		int sum=0;
		for(int i=0;i<str.length()-1;i++){
			char c=str.charAt(i);
			if((int)c<(int)'0'&&(int)c>(int)'9'){
				res=false;
				break;
			}else{
				sum+=((int)c-(int)'0')*arrQz[i];
			}
		}
		if(res){
			int z=sum%11;
			if(arrM[z]!=str.charAt(str.length()-1)){
				res=false;
			}
		}
		return res;
	}

}


发表于 2015-09-18 22:27:00 回复(1)

到底怎么才能变快-.-是不是没循环了

#include <stdio.h>
int main(){
 char ID[100][19], N, i, j = 0, Z[11] = {'1', '0', 'X', '9', '8', '7', '6', '5', '4', '3', '2'}, count[100];
 scanf("%d", &N);
 getchar();
 for (i = 0; i < N; i++){
  scanf("%s", ID[i]);
  getchar();
  if(Z[((ID[i][0] - 48)*7+(ID[i][1] - 48)*9+(ID[i][2] - 48)*10+(ID[i][3] - 48)*5+(ID[i][4] - 48)*8+(ID[i][5] - 48)*4+(ID[i][6] - 48)*2+(ID[i][7] - 48)+(ID[i][8] - 48)*6+(ID[i][9] - 48)*3+(ID[i][10] - 48)*7+(ID[i][11] - 48)*9+(ID[i][12] - 48)*10+(ID[i][13] - 48)*5+(ID[i][14] - 48)*8+(ID[i][15] - 48)*4+(ID[i][16] - 48)*2 )%11] != ID[i][17]){
   count[j] = i;
   j++;
  }
 }
 for (i = 0; i < j; i++)
  printf("%s\n", ID[count[i]]);
 if (j == 0)
  puts("All passed");
 return 0; 
}

发表于 2017-08-08 21:07:31 回复(2)
#include <iostream>
using namespace std;
//检查身份证
int main()
{
    int i,j,error=0,k=0,z;
    int Q[17] = {7,9,10,5,8,4,2,1,6,3,7,9,10,5,8,4,2};
    char M[11] =  {'1','0','X','9','8','7','6','5','4','3','2'};
    int n;
    cin>>n;
    string pass[n+5];
    string s;
    for(i=0; i<n; i++)
    {
        cin>>s;
        if(s.find('X')!=-1 && s.find('X')!=17 )//如果第十七位前出现X s存入数组
        {
            pass[k++]=s;
            error++;
        }
        else
        {
            z=0;
            for(j=0; j<17; j++)
            {
                int x = (s[j]-'0');//char 转 int
                z+=(Q[j]*x);
            }
            if(M[z%11]!=s[17])//校验失败 s存入数组
            {
                pass[k++]=s;
                error++;
            }
        }
    }
    if(error==0) cout<<"All passed"<<endl;
    else{
            for(i=0; i<error; i++) cout<<pass[i]<<endl;
            
    }
}
发表于 2023-08-12 16:01:59 回复(0)
大致思路:将权值以及验证码放在两个数组中;先判断17位数中是否会出错然后如果有错误直接输出然后break 下一个,然后如果不出错,就将其乘以权值然后相加sum总和中,然后sum%11,再利用最后一位数判断是否会相等,不相等就将其输出,相等则用一个cet++,再判断cet会不会等于n等于就输出all passed
#include<bits/stdc++.h>
using namespace std;
const int N = 1e5+5;
char a[11] = {'1','0','X','9','8','7','6','5','4','3','2'};
int b[17] = {7,9,10,5,8,4,2,1,6,3,7,9,10,5,8,4,2};
int main(){
	int n,cet=0,num;
	cin >> n;
	num=n;
	while(n--){
		string s;
		cin >> s;
		int sum=0,flag=0;
		int l = s.length();
		for(int i=0;i<l-1;i++){
			if(s[i]>'9'&&s[i]<'0'){
				 flag = 1;
				 break;
			}
			else{
				int temp = s[i] - '0';
				sum += temp * b[i];
			}
		}
		if(flag) cout << s <<endl;
		else{
			int t = sum%11;
			if(s[l-1]!=a[t]) cout<<s <<endl;
			else 	cet++;
		}	
	}
	if(cet==num){
		cout <<"All passed"	<<endl;
	}
	return 0;
} 

发表于 2023-03-22 20:39:12 回复(0)