PATB1031:查验身份证

来源:互联网 发布:做淘宝目前什么好做啊 编辑:程序博客网 时间:2024/06/06 13:07

PATB1031:查验身份证

【思路】:

输入 遍历每个字符 分类 不符合的用flag标记 通过标记输出结果

1、含有X的

2、不含有X 但是验证不通过的 都是用标记flag

3、通过标记输出结果

【知识点】

如果中途break j < N ,通过循环变量判断来实现分类

> ` for (j = 0; j < 17; j++)//遍历一个字符串完成        {               int t = str[j] - '0';            bit[index++] = t;            if (t< 0 || t> 9)            {                break;            }                   last += bit[j] * w[j];        }        if(j<17){            flag=false;            cout << str << endl;        }`

【知识点】

也可以用Eount来记录,如果出现坏掉了就Ecount++

【知识点】

通过设置flag来分类输出。

  if (flag==true)    {        cout << "All passed" << endl;    }

【参考代码】

#include <cstdio>#include <iostream>#include <cstring> using namespace std;//B1031 查验身份证 //考察点 分类 ,hash ,for的break ,做标记int w[20] = { 7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2 };char changed[20] = { '1', '0', 'X', '9', '8', '7', '6', '5', '4', '3', '2' };void B1031(){    int n;    string str;//每次循环的临时    cin >> n;    int bit[20];//数字    //int Ecount = 0;//错误的个数记录    int flag=true;    for (int i = 0; i < n; i++)    {        int last = 0;        int index = 0;        cin >> str;        int j=0;        for (j = 0; j < 17; j++)//遍历一个字符串完成        {               int t = str[j] - '0';            bit[index++] = t;            if (t< 0 || t> 9)            {                //Ecount++;                //flag=false;                //cout << str << endl;                break;            }                   last += bit[j] * w[j];        }        if(j<17){            flag=false;            cout << str << endl;        }        else if (changed[last % 11] != str[17])        {            //Ecount++;            flag=false;            cout << str << endl;        }    }    if (flag==true)    {        cout << "All passed" << endl;    }}int main(){    B1031();    return 0;}
0 0
原创粉丝点击