본문 바로가기
study

study - day08

by hj_it 2024. 4. 1.

2024.03.27

코딩테스트

[SWEA] JAVA 2단계

 

수도 요금 경쟁 [SWEA  1284]

1) Scanner를 이용해 입력 받음.

import java.util.*;
import java.io.*;
// A, B 두 개의 수도회사중 수도 요금이 저렴한 회사를 선택
// A사 : 1리터당 P원의 돈을 내야 한다.
// B사 : 기본 요금이 Q원이고, 월간 사용량이 R리터 이하인 경우 요금은 기본 요금만 청구된다. 
// 하지만 R 리터보다 많은 양을 사용한 경우 초과량에 대해 1리터당 S원의 요금을 더 내야 한다.
class Solution {
    public static void main(String args[]) throws Exception {
        Scanner sc = new Scanner(System.in);
        int T;
        T=sc.nextInt();
 
        for(int test_case = 1; test_case <= T; test_case++)  {
            int P = sc.nextInt(); // 숫자를 하나씩 인식함.
            int Q = sc.nextInt();
            int R = sc.nextInt();
            int S = sc.nextInt();
            int W = sc.nextInt();
             
            int a = P * W;
            int b = 0;
            if ( R < W) {
                b = Q + ((W - R) * S);
            } else { b = Q;}
         
            if( a <= b) {
                System.out.println("#" + test_case + " " + a);
            } else {
                System.out.println("#" + test_case + " " + b);
            }            
        }
    }
}

 

2) BufferedReader를 이용하여 입력받고, BufferedWriter을 이용해 출력함.

import java.util.*;
import java.io.*;
import java.util.StringTokenizer;

class Solution {
    public static void main(String args[]) throws Exception {
        BufferedReader bf = new BufferedReader(new InputStreamReader(System.in)); //읽는 라인  
        BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out)); //출력 라인  
        int T;
        T= Integer.parseInt(bf.readLine());
       
        for(int test_case = 1; test_case <= T; test_case++)  {
            String s = bf.readLine();
            StringTokenizer st = new StringTokenizer(s);  // 공백을 구분하고자 사용함.
            String arr[] = s.split(" ");
            int P = Integer.parseInt(arr[0]);
            int Q = Integer.parseInt(arr[1]);
            int R = Integer.parseInt(arr[2]);
            int S = Integer.parseInt(arr[3]);
            int W = Integer.parseInt(arr[4]);
             
            int a = P * W;
            int b = 0;
            if ( R < W) {
                b = Q + ((W - R) * S);
            } else { b = Q; }
         
            if( a <= b ) {
                bw.write(String.valueOf("#" + test_case + " " + a));
            } else {
                bw.write(String.valueOf("#" + test_case + " " + b));
            }
            bw.newLine();
        }
        bw.flush();
        bw.close();
    }
}

 

'study' 카테고리의 다른 글

study - day10  (0) 2024.04.01
study - day09  (0) 2024.04.01
study - day07  (1) 2024.03.25
study - day02  (0) 2024.03.13
study - day01  (0) 2024.03.12