[c]构造合法身份证

2016-6-27 写技术

#include <stdio.h>
#define LEN     18
void main(){
        char id[LEN] = {0};
        int weight[] = {7,9,10,5,8,4,2,1,6,3,7,9,10,5,8,4,2};
        int validate[] = {1,0,'x'-'0',9,8,7,6,5,4,3,2,1}; 
        int i=0;
        int sum = 0;
        printf("Input your id\n");      
        for(i=0; i<LEN; i++){
                id[i] = getchar()-'0';  
                sum += id[i]*weight[i];
        }
        sum %= 11;
        printf("Your id is %s [%c]\n", validate[sum] == id[LEN-1]?"valide":"invalide", validate[sum]+'0');
}
~          

标签: C

发表评论:

Powered by anycle 湘ICP备15001973号-1