본문 바로가기

Algorithm/Algospot

[algospot]CONVERT

문제

https://algospot.com/judge/problem/read/CONVERT

Metric 과 English 측정 시스템 변환은 간단하다. 변환프로그램을 작성하라!

kg->lb lb->kg l->g g->l

 

입력

소수 + 공백 + 단위[kg,lb,l,g]

 

출력

라인넘버 + 공백 + 변환된값(소수점 4째자리까지표현)

 

풀이

단위변환을 위해서 값을 곱해주고 출력.단위문자열과 곱해야할 배수는 배열로 지정해놓는다.

 

package com.tutorial;
import java.io.BufferedReader;
import java.io.InputStreamReader;
public class CONVERT {
    public static void main(String[] args)throws Exception {
        BufferedReader br = new BufferedReader(new InputStreamReader(System. in));
        String[] list = {
            "kg",
            "lb",
            "l",
            "g"
        };
        Double[] n = {
            2.2046 d,
            0.4536 d,
            0.2642 d,
            3.7854 d
        };
        int cnt = Integer.parseInt(br.readLine());
        int total = cnt;
        while (cnt -- > 0) {
            String[] input = br.readLine().split(" ");
            double a = Double.parseDouble(input[0]);
            int i = 0;
            for (i = 0; i < list.length; i ++) {
                if (list[i].equals(input[1])) {
                    a *= n[i];
                    break;
                }
            }
            System.out.printf("%d %.4f %s\n", total - cnt, a, list[
                (i % 2 == 0)
                    ? i + 1
                    : i - 1
            ]);
        }
    }
}

 

'Algorithm > Algospot' 카테고리의 다른 글

[algospot]XHAENEUNG  (0) 2014.12.11
[algospot]HOTSUMMER  (0) 2014.12.11
[algospot]MISPELL  (0) 2014.12.11
[algospot]ENCRYPT  (0) 2014.12.11
[algospot]LECTURE  (0) 2014.12.11