zoukankan      html  css  js  c++  java
  • c++ 字符串

    /*

    一个合法的身份证号码由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
    */
    #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;
    }

  • 相关阅读:
    Systemd 入门教程:实战篇
    Systemd 入门教程:命令篇
    awk详解
    yum Multilib version problems
    Oracle ORA 12541 报错解决过程
    创建表空间
    大文件按行分离脚本
    heartbeat安装与配置
    Keepalived安装配置
    速查表的名称和建表语句
  • 原文地址:https://www.cnblogs.com/qingtianBKY/p/6758905.html
Copyright © 2011-2022 走看看