2014年3月20日 星期四

Java 電話客服中心

題目:電話客服中心


import java.util.Scanner;

public class IdAuthentication{
    public static void main(String[] args){
        Scanner sc = new Scanner(System.in);
        String str;
  
        while( sc.hasNext() ){
            str = sc.nextLine();
            char c[] = str.toCharArray();
   
            int sum = 0;
            for(int i = 0; i < 8; i++ ){
                sum += (c[i]-48)*(8-i);
            }
            sum += (c[8]-48);
   
            int n;
            for(int a = 65; a <= 90; a++){
                if( a == 88 || a == 89 ){
                    n = a - 58;
                }
                else if( a >= 65 && a <= 72 ){
                    n = a - 55;
                }else if( a >= 74 && a <= 78){
                    n = a - 56;
                }else if( a >= 80 && a <= 86){
                    n = a - 57;
                }else{
                    n = (a == 73) ? 34 : (a == 79) ? 35 : 
                        (a == 87) ? 32 : (a == 90) ? 33 : a;
                }
                if( (sum + (n/10) + ((n%10)*9))%10 == 0 ){
                    System.out.print((char)a);
                }
            }
            System.out.println();
        }
    }
}

Java 判斷質數

判斷質數

輸入: 2 < n < 2147483647


import java.util.Scanner;
import java.math.BigInteger;

public class Prime {
  
    public static boolean MillerRabin(BigInteger x, int d, int time, BigInteger n){
        BigInteger nDivOne = n.subtract( BigInteger.ONE );
        BigInteger TWO = BigInteger.valueOf(2);
        x = x.modPow(BigInteger.valueOf(d), n);
  
        // x = a ^ d mod n,若x=1 or x=n-1,直接跳過
        if( !x.equals( BigInteger.ONE ) && !x.equals( nDivOne ) ){
   
            //在S-1的次數內,x = x ^ 2 mod n,若x=n-1 直接跳過
            while( --time > 0 ){
                x = x.modPow(TWO, n);
                if( x.equals( nDivOne ) ){ return true; }
            }
            return false;
        }
        return true;
 }
 
    public static boolean isPrime(int n){  
        // (x ^ s) * d = n - 1
        int d = n - 1;
        int s = 0;
        while( d % 2 == 0 ){
            d /= 2;
            s++;
        }
  
        return MillerRabin(BigInteger.valueOf(2), d, s, BigInteger.valueOf(n)) &&
               MillerRabin(BigInteger.valueOf(7), d, s, BigInteger.valueOf(n)) &&
               MillerRabin(BigInteger.valueOf(61), d, s, BigInteger.valueOf(n));
    }

    public static void main(String[] args){
        Scanner sc = new Scanner(System.in);
        int num;
  
        while( sc.hasNext() ){
            try{
                num = sc.nextInt();
            }catch(java.util.InputMismatchException e){
                break;
            }
    
            switch( num ){
                case 2: case 5: case 7: case 61:
                    System.out.println( "質數" );
                    break;
                default:
                    switch( num % 10 ){
                        case 1: case 3: case 7: case 9:
                            System.out.println( isPrime(num) ? "質數" : "非質數" );
                            break;
                        default:
                            System.out.println( "非質數" );
                        break;
                    }
                    break;
            }
        }
     }
}


寫完之後,發現記憶體用量異常的大,不知道是不是因為使用BigInteger物件的關係,哈