2012年3月5日 星期一

Java 羅馬數字


import java.util.*;

public class RomeNumber{
        public static void main(String[] args){
                Scanner sc = new Scanner(System.in);
  
                while(sc.hasNext()){
                        String str [] = sc.nextLine().split(" ");
                        if(str[0].equals("#")){
                                break;
                        }

                        int a = transformIntNumber(str[0]);
                        int b = transformIntNumber(str[1]);
                        int sum = Math.abs(a - b);

                        if( sum == 0){
                                System.out.println("ZERO");
                        }
                        else{
                                System.out.println(transformRomeNumber(sum));
                        }
                }
        } 

        public static int transformIntNumber(String s){
                int count = 0;
                int a [] = new int [s.length()];
                for(int i = 0; i < a.length; i++){
                        switch(s.charAt(i)){
                                case 'I':
                                        a[i] = 1;
                                        break;
                                case 'V':
                                        a[i] = 5;
                                        break;
                                case 'X':
                                        a[i] = 10;
                                        break;
                                case 'L':
                                        a[i] = 50;
                                        break;
                                case 'C':
                                        a[i] = 100;
                                        break;
                                case 'D':
                                        a[i] = 500;
                                        break;
                                case 'M':
                                        a[i] = 1000;
                                        break;
                        }
                }
  
                for(int i = 0; i < a.length; i++){
                        if( i == (a.length - 1)){
                                count += a[i];
                                break;
                        }
                        else if( a[i] < a[i+1] ){
                                count -= a[i];
                        }
                        else{
                                count += a[i];
                        }
                }
  
                return count;
        }

        public static String transformRomeNumber(int a){
                String str = "";
                int index;
                String rome [] = {"I","V","X","L","C","D","M"};
                int num [] = {1,5,10,50,100,500,1000};
                Arrays.sort(num);

                while( a > 0 ){
                        index = Arrays.binarySearch(num, a);
                        if(index == -8){
                                index = 6;
                        }else if(index < 0){
                                index = -1 * index -2;
                                int i = index / 2 * 2;
                                if(a + num[i] >= num[index + 1]){
                                        str += rome[i];
                                        a += num[i];
                                        index++;
                                }
                        }
                        str += rome[index];
                        a -= num[index];
                }
  
                return str;
        }
}

題目
參考程式

沒有留言:

張貼留言