#把15位身份证号放入字符串s中 可得到输出 s = '11010519491231002x'#测试数据 s = '440524188001010014'#测试数据 s = '110105491231002' s1 = s.slice(0,6) s2 = s.slice(6,9) s = s1 + '19' + s2 puts s wi = [7,9,10,5,8,4,2,1,6,3,7,9,10,5,8,4,2] idnum = Array.new for i in (0..16) idnum[i] = s.slice(i,1).to_i end sum = 0 for i in (0..16) sum = sum + (idnum[i]*wi[i]) end tmps = sum.modulo(11) IDNUMTAB={ 0=>'1', 1=>'0', 2=>'X', 3=>'9', 4=>'8', 5=>'7', 6=>'6', 7=>'5', 8=>'4', 9=>'3', 10=>'2' } puts IDNUMTAB[tmps] s = s.slice(0,17) + IDNUMTAB[tmps] puts s