输入第一行给出正整数N(<= 100)是输入的身份证号码的个数。随后N行,每行给出1个18位身份证号码。
按照输入的顺序每行输出1个有问题的身份证号码。这里并不检验前17位是否合理,只检查前17位是否全为数字且最后1位校验码计算准确。如果所有号码都正常,
则输出“All passed”。
4 320124198808240056 12010X198901011234 110108196711301866 37070419881216001X
12010X198901011234 110108196711301866 37070419881216001X
#ifndef Basic_Level_1021_CPP
#define Basic_Level_1021_CPP
#include<iostream>
#include<string>
using namespace std;
int main()
{
int weight[17]={7,9,10,5,8,4,2,1,6,3,7,9,10,5,8,4,2};
char M[11]={'1','0','x','9','8','7','6','5','4','3','2'};
int N;
cin>>N;
string A;
int flag=1;//whether the data is all passed
for(int i=0;i<N;++i)
{
int sum=0;
int is_wrong=0;
cin>>A;
for(int j=0;j<17;++j)
{
if(A[j]<'0'||A[j]>'9')
{
cout<<A<<endl;
flag=0;
is_wrong=1;
break;
}
sum+=(A[j]-'0')*weight[j];
}
//judge the last char
int z=sum%11;
char right=M[z];
if(A[17]!=right&&is_wrong==0)
{
flag=0;
cout<<A<<endl;
}
}
if(flag)
{
cout<<"All passed"<<endl;
}
return 0;
}
#endif // Basic_Level_1021_CPP
import java.util.*;
/**
* 查验身份证
* 题目描述
* 一个合法的身份证号码由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
* 输出例子:
* 12010X198901011234
* 110108196711301866
* 37070419881216001X
*
* @author shijiacheng
* @date 2018/1/29
*/
public class B1021CheckIDCard {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int[] weight = {7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2};
Map<Integer, String> vertify = new HashMap<>();
for (int i = 0; i < 11; i++) {
if (i < 2) {
vertify.put(i, String.valueOf(1 - i));
} else if (i == 2) {
vertify.put(i, "X");
} else {
vertify.put(i, String.valueOf(12 - i));
}
}
int N = sc.nextInt();
List<String> list = new ArrayList<>();
for (int i = 0; i < N; i++) {
list.add(sc.next());
}
List<String> errorList = new ArrayList<>();
for (int i = 0; i < list.size(); i++) {
if (list.get(i).length() == 18) {
if (list.get(i).contains("X")) {
if (list.get(i).charAt(17) == 'X') {
if (!vertify(list.get(i), weight, vertify)) {
errorList.add(list.get(i));
}
} else {
errorList.add(list.get(i));
}
} else {
if (!vertify(list.get(i), weight, vertify)) {
errorList.add(list.get(i));
}
}
} else {
errorList.add(list.get(i));
}
}
if (errorList.size() == 0) {
System.out.println("All passed");
} else {
for (int i = 0; i < errorList.size(); i++) {
System.out.println(errorList.get(i));
}
}
}
public static boolean vertify(String id, int[] weight, Map<Integer, String> vertify) {
int sum = 0;
for (int i = 0; i < 17; i++) {
int temp = id.charAt(i) - '0';
sum += temp * weight[i];
}
int result = sum % 11;
if (vertify.get(result).equals(String.valueOf(id.charAt(17)))) {
return true;
} else {
return false;
}
}
}
#include<iostream>
using namespace std;
int w[17]={7,9,10,5,8,4,2,1,6,3,7,9,10,5,8,4,2};
int M[11]={1,0,'X'-'0',9,8,7,6,5,4,3,2};
bool valid(string s){
int Z,sum=0;
for(int i=0;i<17;i++){
if(s[i]<'0'||s[i]>'9')return false;
sum+=(s[i]-'0')*w[i];
}
Z=sum%11;
return M[Z]==s[17]-'0';
}
int main() {
bool allpass=true;
int N;
string s;
cin>>N;
while(N--){
cin>>s;
if(!valid(s)){
cout<<s<<endl;
allpass=false;
}
}
if(allpass)cout<<"All passed";
return 0;
}
#include <cstdio>
#include <cstring>
#include <string>
#include <cstdlib>
#include <iostream>
#include <cmath>
#include <algorithm>
using namespace std;//思路:用数组将权值保存,将存入的身份证号与权值一一对应,再求余即可
int a[]= {7,9,10,5,8,4,2,1,6,3,7,9,10,5,8,4,2};
char s[20],c[]= {"10X98765432"};
int main()
{
int n,len1,i,num=0;
scanf("%d",&n);
int m=n;
while(n--)
{
int sum=0;
scanf("%s",s);
len1=strlen(s);
for(i=0; i<len1-1; i++)
{
if('0'>s[i]||s[i]>'9')
{
sum=-1;
break;
}
sum+=(s[i]-'0')*a[i];
}
sum=sum%11;
if(s[len1-1]==c[sum])//每次如果验证码正确就加1
num++;
else
sum=-1;
if(sum==-1)
cout<<s<<endl; }
if(num==m)
printf("All passed");
return 0;
} import java.util.Scanner;
public class Main {
private static final char[] M = {'1','0','X','9','8','7','6','5','4','3','2'};
private static final int[] W = {7,9,10,5,8,4,2,1,6,3,7,9,10,5,8,4,2};
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int n = in.nextInt();
String[] errors = new String[n];
int count = 0;
for(int i = 0;i<n;i++){
String id = in.next();
if(!check(id))
errors[count++] = id;
}
if(count==0)
System.out.println("All passed");
else
for(int i = 0;i<count;i++)
System.out.println(errors[i]);
}
private static boolean check(String id){
if(id.length()!=18)
return false;
int w = 0;
for(int i = 0;i<17;i++){
char c = id.charAt(i);
if(c>='0'&&c<='9')
w += W[i]*(c-'0');
else
return false;
}
return M[w%11]==id.charAt(17);
}
}
#include<bits/stdc++.h>
using namespace std;
int main(){
string check = "10X98765432";
string idNumber;
int weight[17] = {7,9,10,5,8,4,2,1,6,3,7,9,10,5,8,4,2};
int sign , sum , n ;
sign = 0;
cin>>n;
while(n--){
cin>>idNumber;
sum = 0;
for(int i = 0; i < 17; i ++){
if(idNumber[i]<'0' | idNumber[i]>'9')
{
// 检查是否有非数字
cout<<idNumber<<endl;
sign = 1;
break;
}
sum += (idNumber[i] - '0') * weight[i];
}
if(check[sum % 11] != idNumber[17]){
// 计算加权求和值
cout<<idNumber<<endl;
sign = 1;
}
}
if(!sign){
cout<<"All passed";
}
return 0;
} import java.util.*;
public class Main{
public static void main(String []args){
Scanner in=new Scanner(System.in);
int N=in.nextInt();
char c[][]=new char[N][18];
String s[]=new String[N];
int n=0;
for(int i=0;i<N;i++) {
s[i]=in.next();
}
for(int i=0;i<N;i++) {
for(int j=0;j<18;j++) {
c[i][j]=s[i].toCharArray()[j];
}
}
char CheckCode[]={'1', '0', 'X', '9','8', '7', '6', '5', '4', '3', '2'};
int w[]={7,9,10,5,8,4,2,1,6,3,7,9,10,5,8,4,2};
for(int i=0;i<N;i++){
int count=0,sum=0;
for(int j=0;j<17;j++){
if(!Character.isDigit(c[i][j])) {
for(int k=0;k<18;k++){
System.out.print(c[i][k]);
}
System.out.println();
n++;
}
else
{
count++;
if(count==17){
for(int jj=0;jj<17;jj++)
sum+=(c[i][jj]-'0')*w[jj];
if(CheckCode[sum%11]!=c[i][17]) {
for(int k=0;k<18;k++){
System.out.print(c[i][k]);
}
System.out.println();
n++;
}
}
}
}
}
if(n==0) System.out.println("All passed");
}
}
又来分享Python版了
while True:
try:
num = int(input())
idLists = []
checkCode = "10X98765432"
weights = [7,9,10,5,8,4,2,1,6,3,7,9,10,5,8,4,2]
for i in range(num):
idLists.append(input())
wrongResult = []
for i in range(num):
codeSum = 0
if idLists[i][:17].isdigit(): #检查前十七位是否为数字
for j in range(17): #循环加权重
codeSum += weights[j]*int(idLists[i][j])
if checkCode[codeSum%11] != idLists[i][17]:
wrongResult.append(idLists[i])
else:
wrongResult.append(idLists[i])
if len(wrongResult) == 0:
print("All passed")
else:
for i in wrongResult:
print(i)
except Exception:
break
#include <bits/stdc++.h> using namespace std; int main(){ int m; string no; long long int i,t=0,number,sum; int a[12]={1,0,'X',9,8,7,6,5,4,3,2}; int b[19]={7,9,10,5,8,4,2,1,6,3,7,9,10,5,8,4,2}; cin >> m; number=m; while(m--){ cin >> no; i=0; sum=0; while(no[i] != '\0'){ if(no[i] >= '0' && no[i] <= '9'){ if(i < 17) sum+=(no[i] - '0')*b[i]; } else if(i != 17 ){cout << no << endl;break;} if(i == 17){ // cout << sum <<endl; sum%=11; // cout << sum << ' ' << t << endl; if(a[sum] == (no[i]-'0') || sum == 2 && no[i] == 'X'){ t++; } if(a[sum] != no[i]-'0') { if(no[i] == 'X') { if(sum != 2) cout << no << endl; } else cout << no << endl; } } i++; } } if(t == number)cout << "All passed" << endl; return 0; }
//小白第一步,终于成功了,激动到想哭~.~import java.util.*; public class Main{ private static final int[] W={7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2}; private static final char[] M = {'1','0','X','9','8','7','6','5','4','3','2'}; public static void main(String[] args){ Scanner sc = new Scanner(System.in); int N=sc.nextInt(); int count=0; ArrayList<String> errors=new ArrayList<String>(); for(int i=0;i<N;i++){ String id=sc.next(); if (!checkId(id)){ count+=1; errors.add(id); } } if(count != 0){ for(int i=0;i<errors.size();i++){ System.out.println(errors.get(i)); } } else{ System.out.print("All passed"); } } public static boolean checkId(String id){ int sum=0; for (int j=0;j<17;j++){ if((id.charAt(j)>='0') && (id.charAt(j)<='9')){ sum+=W[j]*(id.charAt(j)-'0'); } else{ return false; } } return M[sum%11]==id.charAt(17); } }
# coding=utf-8
#小白的Python版本,开心>.<
W=[7,9,10,5,8,4,2,1,6,3,7,9,10,5,8,4,2]
M=['1','0','x','9','8','7','6','5','4','3','2']
errors=[]
n=int(raw_input())
def Main(n,W,M):
for i in range(n):
id =raw_input()
if not checkId(id,W,M):
errors.append(id)
if len(errors)==0 :
print "All passed"
else:
for i in errors:
print i
def checkId(id,W,M):
sum=0
id_part=id[:17]
for i,c in enumerate(id_part):
if (c>='0') and (c <='9'):
#print c
sum+=W[i]*int(id_part[i])
else:
return False
return M[sum%11]==id[-1]
Main(n,W,M)
#include <iostream>
#include <string.h>
#include <ctype.h>
using namespace std;
class Str
{
public:
char chs[18];
int flag;
void SetStr(char * ch){
memcpy(chs,ch,18);
flag = 1;
}
void isnum()
{
int i;
for(i=0;i<18;i++){
if(!(isdigit(chs[i]))) {
flag = 0;
break;
}
}
}
void checkout()
{
int result=0,i;
char rst;
int a[17] = {7,9,10,5,8,4,2,1,6,3,7,9,10,5,8,4,2};
char corr[11] = {'1','0','X','9','8','7','6','5','4','3','2'};
for(i=0;i<17;i++){
result += (chs[i]-'0')*a[i];
}
result = result % 11;
rst = corr[result];
if (rst != chs[17])
this->flag=0;
}
};
int main()
{
int n,i,count=0;
cin >> n;
Str strings[n];
for(i=0;i<n;i++){
char temp[18];
cin >> temp;
strings[i].SetStr(temp);
strings[i].isnum();
if(strings[i].flag==1)
strings[i].checkout();
if(strings[i].flag==1)
count++;
else
cout << temp <<endl;
}
if(count == n)
cout << "All passed" <<endl;
return 0;
}
import java.util.*;
public class Main {
static int[] weight = {7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2};
static char[] M = {'1', '0', 'X', '9', '8', '7', '6', '5', '4', '3', '2'};
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
while (sc.hasNext()) {
int n = sc.nextInt();
String[] arr = new String[n];
for (int i = 0; i < n; i ++ )
arr[i] = sc.next();
boolean isFinded = false;
for (int i = 0; i < n; i ++ ) {
String s = arr[i];
if(s.substring(0, 17).contains("X")) {
isFinded = true;
System.out.println(s);
} else {
int sum = 0;
for (int j = 0; j < 17; j ++ )
sum += (s.charAt(j) - '0') * weight[j];
if(s.charAt(17) != M[sum % 11]) {
isFinded = true;
System.out.println(s);
}
}
}
if( ! isFinded) System.out.println("All passed");
}
}
}
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace Question21ID { class Program { static void Main(string[] args) { var N = int.Parse(Console.ReadLine()); var ids = Enumerable.Range(0, N).Select(s => Console.ReadLine()).ToArray(); var right = new[] { 7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2 }; var mV = @"10X98765432"; var index = Enumerable.Range(0, 17); var errorcount = ids.Where(item => { return mV[index.Select(s => right[s] * (item[s] - '0')).Sum() % 11]!=item.Skip(17).First(); }); if (errorcount.Any()) { foreach (var item in errorcount) { Console.WriteLine(item); } } else { Console.WriteLine("All passed"); } } } }
#include <iostream> using namespace std; int weight[17] = {7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2}; char M[11] = {'1', '0', 'X', '9', '8', '7', '6', '5', '4', '3', '2'}; bool isValid(string id); int main() { ios::sync_with_stdio(false); int N; cin >> N; int validNum = 0; for(int i=0; i<N; i++) { string id; cin >> id; if(isValid(id)) { validNum++; } else { cout << id << endl; } } if(N == validNum) { cout << "All passed" << endl; } return 0; } bool isValid(string id) { int sum = 0; for(int i=0; i<17; i++) { if(id[i]>='0' && id[i]<='9') { sum += (int)(id[i]-'0')*weight[i]; } else { return false; } } sum %= 11; if(M[sum] == id[17]) { return true; } else { return false; } }
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
public class Main {
static final int[] arrQz={7,9,10,5,8,4,2,1,6,3,7,9,10,5,8,4,2};
static final char[] arrM={'1','0','X','9','8','7','6','5','4','3','2'};
public static void main(String[] args) {
// TODO 自动生成的方法存根
Scanner sc=new Scanner(System.in);
int N=sc.nextInt();
List lStr=new ArrayList<String>();
for(int i=0;i<N;i++){
String str=sc.next();
if(!isHf(str)){
lStr.add(str);
}
}
//System.out.println(lStr.get(0));
if(lStr.isEmpty()){
System.out.print("All passed");
}else{
for(int i=0;i<lStr.size();i++){
System.out.println(lStr.get(i));
}
}
}
public static boolean isHf(String str){
//int num=Integer.parseInt(str);
boolean res = true;
int sum=0;
for(int i=0;i<str.length()-1;i++){
char c=str.charAt(i);
if((int)c<(int)'0'&&(int)c>(int)'9'){
res=false;
break;
}else{
sum+=((int)c-(int)'0')*arrQz[i];
}
}
if(res){
int z=sum%11;
if(arrM[z]!=str.charAt(str.length()-1)){
res=false;
}
}
return res;
}
}
到底怎么才能变快-.-是不是没循环了
#include <stdio.h>
int main(){
char ID[100][19], N, i, j = 0, Z[11] = {'1', '0', 'X', '9', '8', '7', '6', '5', '4', '3', '2'}, count[100];
scanf("%d", &N);
getchar();
for (i = 0; i < N; i++){
scanf("%s", ID[i]);
getchar();
if(Z[((ID[i][0] - 48)*7+(ID[i][1] - 48)*9+(ID[i][2] - 48)*10+(ID[i][3] - 48)*5+(ID[i][4] - 48)*8+(ID[i][5] - 48)*4+(ID[i][6] - 48)*2+(ID[i][7] - 48)+(ID[i][8] - 48)*6+(ID[i][9] - 48)*3+(ID[i][10] - 48)*7+(ID[i][11] - 48)*9+(ID[i][12] - 48)*10+(ID[i][13] - 48)*5+(ID[i][14] - 48)*8+(ID[i][15] - 48)*4+(ID[i][16] - 48)*2 )%11] != ID[i][17]){
count[j] = i;
j++;
}
}
for (i = 0; i < j; i++)
printf("%s\n", ID[count[i]]);
if (j == 0)
puts("All passed");
return 0;
} #include<bits/stdc++.h>
using namespace std;
const int N = 1e5+5;
char a[11] = {'1','0','X','9','8','7','6','5','4','3','2'};
int b[17] = {7,9,10,5,8,4,2,1,6,3,7,9,10,5,8,4,2};
int main(){
int n,cet=0,num;
cin >> n;
num=n;
while(n--){
string s;
cin >> s;
int sum=0,flag=0;
int l = s.length();
for(int i=0;i<l-1;i++){
if(s[i]>'9'&&s[i]<'0'){
flag = 1;
break;
}
else{
int temp = s[i] - '0';
sum += temp * b[i];
}
}
if(flag) cout << s <<endl;
else{
int t = sum%11;
if(s[l-1]!=a[t]) cout<<s <<endl;
else cet++;
}
}
if(cet==num){
cout <<"All passed" <<endl;
}
return 0;
}