Love丶FFC's Blog

PAT乙级:1031 查验身份证

2020-02-21 10:52:05
阅读:1452   •   评论:18
标签:,

一个合法的身份证号码由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

输出样例1:

12010X198901011234
110108196711301866
37070419881216001X

输入样例2:

2
320124198808240056
110108196711301862

输出样例2:

All passed

编程语言:Python

解题思想:

1.使用列表和字典来存放权重、校验码的对应关系

2.首先判断前17位是否都是数字,使用isnumeric方法,如果是,则求出权重和并判断与校验码是否匹配

3.设置一个标记,在整个循环过程中,只要有一个身份证号不正确, 就改变标记,为最后的输出提供判断

时间复杂度:O(N),虽然是双层循环,但内循环为固定常数

代码如下:

  1. NOI = int(input())
  2. Weights = [7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2] # 每一位号码对应的权重
  3. CheckCode = { # 利用字典来存放Z值与校验码的对应关系
  4. 0: '1',
  5. 1: '0',
  6. 2: 'X',
  7. 3: '9',
  8. 4: '8',
  9. 5: '7',
  10. 6: '6',
  11. 7: '5',
  12. 8: '4',
  13. 9: '3',
  14. 10: '2'
  15. }
  16.  
  17. Judge = True # 所有身份证号是否都正确的标记,默认为是
  18. for i in range(NOI):
  19. Identity = input() # 输入身份证号
  20. Z = 0
  21. if Identity[:17].isnumeric() == True: # 该身份证号的前17位全为数字
  22. for j in range(17): # 求权重和
  23. Z += int(Identity[j]) * Weights[j]
  24. Z %= 11
  25. if CheckCode[Z] != Identity[17]: # Z值对应校验码与身份证号最后一位不匹配
  26. print(Identity)
  27. Judge = False
  28. else:
  29. Judge = False
  30. print(Identity)
  31.  
  32. if Judge == True: # 所有身份证号都正确
  33. print("All passed")

评论板

共有 18 条评论

  1. Abupguele

    cialis 20mg Federal Register, 56, 6426 6505

  2. cheap louis vuitton bags

    I just wanted to thank you for the fast service. and it could be they look great. I received them a day earlier than expected. like I will definitely continue to buy from this site. you decide I will recommend this site to my friends. Thanks!
    cheap louis vuitton bags https://www.louisvuittonsoutletstore.com/

  3. authentic louis vuitton outlet

    I just wanted to thank you for the fast service. or they look great. I received them a day earlier than expected. prefer I will definitely continue to buy from this site. in any event I will recommend this site to my friends. Thanks!
    authentic louis vuitton outlet https://www.cheapreallouisvuitton.com/

  4. authentic cheap jordans

    I just wanted to thank you for the fast service. to they look great. I received them a day earlier than expected. much I will definitely continue to buy from this site. in either case I will recommend this site to my friends. Thanks!
    authentic cheap jordans https://www.realcheapretrojordanshoes.com/

  5. cheap jordan shoes

    I just wanted to thank you for the fast service. as well they look great. I received them a day earlier than expected. cherish the I will definitely continue to buy from this site. in any event I will recommend this site to my friends. Thanks!
    cheap jordan shoes https://www.realjordansretro.com/

  6. louis vuitton outlet

    I just wanted to thank you for the fast service. to they look great. I received them a day earlier than expected. love the I will definitely continue to buy from this site. blue jays I will recommend this site to my friends. Thanks!
    louis vuitton outlet https://www.bestlouisvuittonoutlet.com/

  7. Knitant

    Preclinical studies have harder erections levitra mieux que viagra

  8. cheap jordans for sale

    Read reviews and was a little hesitant since I had already inputted my order. and also but thank god, I had no issues. as good as the received item in a timely matter, they are in new condition. an invaluable so happy I made the purchase. Will be definitely be purchasing again.
    cheap jordans for sale https://www.realjordansshoes.com/

  9. authentic louis vuitton outlet

    Read reviews and was a little hesitant since I had already inputted my order. also but thank god, I had no issues. choose to received item in a timely matter, they are in new condition. no matter what so happy I made the purchase. Will be definitely be purchasing again.
    authentic louis vuitton outlet https://www.louisvuittonsoutletonline.com/

  10. invoimi

    Ischemic optic neuropathy and inherited degenerative retinal disorders contraindicate sildenafil use; discontinuation of sildenafil is recommended if a sudden vision loss occurs fincar 5mg

  11. invoimi

    where to buy viagra connect Presence of a regularly gallop rhythm d

  12. tZbGiTJJ

    102 Combinations between tucatinib and monoclonal antibodies such as trastuzumab have been recently developed for the treatment of HER2 metastatic BC patients soft tab cialis

  13. Rpejek

    purchase tricor online cheap buy fenofibrate generic tricor price

  14. XZsoTW

    The AUC of ertugliflozin decreased by approximately 13, and Cmax decreased by approximately 21 compared to subjects with normal hepatic function how do i get viagra pills I ve been doing some research on The Colorado Experiment and came across this routine

  15. Qixbmw

    tadalafil sale viagra drug purchase sildenafil generic

  16. Wzzuul

    generic ketotifen 1 mg buy cheap ketotifen buy generic imipramine for sale

  17. Oebxer

    order mintop sale cost tamsulosin 0.4mg herbal ed pills

  18. Xgexqz

    precose medication order acarbose 25mg generic fulvicin without prescription

--------查看该分类下最新文章--------
^
新版博客正在完善中!域名:http://www.loveffc:8080,点击跳转,完全移植后将去除端口号。

Copyright © 2018 - 2021 FFC的小站 - 滇 ICP 备 18010780 号 - 1

- Powered by WordPress & AliYun · Theme by FFC -

- Environment by Windows & XAMPP · Designed by WebStorm & VSCode -

已运行:

访问量:510122