BUPT-homework/semester1/pset12/2-2-IDnumber.c
2021-12-05 12:16:06 +08:00

44 lines
761 B
C

#include <stdio.h>
#include <stdlib.h>
#pragma GCC optimize("Ofast,inline")
#define MAXLEN 18
// returns 1 if valid, otherwise 0
int validate(char *id);
int main(void) {
char *id = malloc(sizeof(char) * MAXLEN);
int lines;
scanf("%d", &lines);
for (int i = 0; i < lines; i++) {
scanf("%s", id);
if (validate(id)) {
printf("right\n");
} else {
printf("wrong\n");
}
}
free(id);
return 0;
}
int validate(char *id) {
char lasttable[] = "10X98765432";
int factor[] = {7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2};
int last = 0;
for (int i = 0; i < 17; i++) {
last += (id[i] - '0') * factor[i];
}
last = last % 11;
if (lasttable[last] == id[17]) {
return 1;
} else {
return 0;
}
}