a020: 身分證檢驗

題目:http://zerojudge.tw/ShowProblem?problemid=a020
如果程式碼又更好想法可以分享喔~~
程式碼:

import java.util.Scanner;
public class a020 {

 public static void main(String[] args) {
  // TODO Auto-generated method stub
  Scanner sc = new Scanner(System.in);
  char [] data = {'A','B','C','D','E','F','G','H','I','J','K','L'
    ,'M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z'};
  String [] data1 = {"10","11","12","13","14","15","16","17","34","18",
    "19","20","21","22","35","23","24","25","26","27","28","29","32"
    ,"30","31","33"};
  while(sc.hasNext()){
   String id = sc.nextLine();
   char var = id.charAt(0);
   char [] b = new char[9];
   String a="";
   int num=0;
   for(int i=0;i<data.length;i++){
    if(var == data[i]){
     a +=  data1[i];
    }
   }
   num += ((int)a.charAt(1)-48)*9;
   num += ((int)a.charAt(0)-48);
   for(int i=0;i<id.length()-1;i++){
    b[i] = id.charAt(i+1);//還是char型態
    for(int j=8;j>=0;j--){
     if(i+j==8){
      num += (int)(b[i]-48)*j;
      if(j==0){
       num += (int)(b[i]-48);
      }
     }
    }
   }
   if(num%10==0){
    System.out.println("real");
   }else{
    System.out.println("fake");
   }
   
  }
 }

}

留言

熱門文章