'convert'에 해당되는 글 1건

  1. 2021.12.27[CONVERT] Conversions

Java 로 풀어보는 알고리즘입니다. 📖
코딩테스트를 대비하여 JAVA1.8 부터 제공되는 함수형 API 는 사용하지 않았습니다.

문제 : https://www.algospot.com/judge/problem/read/CONVERT

 

algospot.com :: CONVERT

Conversions 문제 정보 문제 Conversion between the metric and English measurement systems is relatively simple. Often, it involves either multiplying or dividing by a constant. You must write a program that converts between the following units: Type M

www.algospot.com

 

풀이입니다. 🤔

 

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
import java.util.*;
 
public class Main {
    private static final String KG = "kg";
    private static final String L = "l";
    private static final String LB = "lb";
    private static final String G = "g";
 
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int cases = scanner.nextInt();
        for (int i = 1; i <= cases; ++i) {
            double number = scanner.nextDouble(), convertNumber;
            String unit = scanner.next(), convertUnit;
            switch (unit) {
                case KG:
                    convertNumber = 2.2046 * number;
                    convertUnit = LB;
                    break;
 
                case L:
                    convertNumber = 0.2642 * number;
                    convertUnit = G;
                    break;
 
                case LB:
                    convertNumber = 0.4536 * number;
                    convertUnit = KG;
                    break;
 
                case G:
                    convertNumber = 3.7854 * number;
                    convertUnit = L;
                    break;
 
                default:
                    throw new IllegalArgumentException();
            }
 
            System.out.printf("%d %.4f %s\n", i, convertNumber, convertUnit);
        }
    }
}
cs



이 포스트를 읽어주셔서 감사합니다. 🙇🏻‍♂️

반응형

'개발이야기 > 알고스팟' 카테고리의 다른 글

[BOARDCOVER] 게임판 덮기  (0) 2021.12.28
[HOTSUMMER] 에어컨을 끈다고 전력난이 해결될까?  (0) 2021.12.27
[PICNIC] 소풍  (0) 2021.12.27
[ENCRYPT] 문자열 암호화  (0) 2021.12.27
[LECTURE] Lecture Note  (0) 2021.12.27
Posted by N'