← 返回题库
初级

查验身份证

未完成
初级参考 完整示例代码供参考,建议自己理解后重新输入
def solve(n, data):
    n = int(n)
    ids_list = str(data).strip().split(',')
    weights = [7,9,10,5,8,4,2,1,6,3,7,9,10,5,8,4,2]
    check_map = {0:'1',1:'0',2:'X',3:'9',4:'8',5:'7',6:'6',7:'5',8:'4',9:'3',10:'2'}
    bad = []
    for id_str in ids_list[:n]:
        id_str = id_str.strip()
        try:
            s = sum(int(id_str[i]) * weights[i] for i in range(17))
        except:
            bad.append(id_str)
            continue
        expected = check_map[s % 11]
        if id_str[17] != expected:
            bad.append(id_str)
    if bad:
        for b in bad:
            print(b)
    else:
        print('All passed')

示例

输入
4|320124198808240056,12010X198901011234,110108196711301866,37070419881007002X
期望输出
12010X198901011234
110108196711301866
37070419881007002X
Python 代码 🔒 登录后使用
🔒

登录后即可练习

注册免费账号,在浏览器中直接运行 Python 代码