SWEA 1284 수도 요금 경쟁

Jmnote (토론 | 기여)님의 2023년 8월 25일 (금) 02:13 판 (→‎Python)
(차이) ← 이전 판 | 최신판 (차이) | 다음 판 → (차이)

1 개요[ | ]

SWEA 1284 수도 요금 경쟁
  • SW Expert Academy 1284번 문제
  • SWEA D2
  • A의 비용은 항상 W * P 이고 B의 비용은 W가 R보다 큰 경우와 작은 경우를 나누어 생각한다.
  • 이 3가지를 비교하여 가장 작은 값을 출력한다.

2 C++[ | ]

#include<iostream>

using namespace std;

int main() {
    int T, test, P, Q, R, S, W;
    
    cin >> T;

    for (test = 1; test <= T; test++) {
        cin >> P >> Q >> R >> S >> W;
       
        int A, B;
        A = P * W;
        B = Q;
        if (R < W)
            B += (W - R) * S;
        A = (A < B) ? A : B;
        cout << '#' << test << ' ' << A << '\n';
    }
    return 0;
}

3 Java[ | ]

import java.util.*;
class Solution {
    public static void main(String args[]) {
        Scanner sc = new Scanner(System.in);
        int T, x;
        T = sc.nextInt();
        
        int P, Q, R, S, W;
        int A, B, charge;
        for(x=1; x<=T; x++) {
            P = sc.nextInt();
            Q = sc.nextInt();
            R = sc.nextInt();
            S = sc.nextInt();
            W = sc.nextInt();

            A = P * W;
            if( W <= R ) B = Q;
            else B = Q + ( W-R ) * S;
            
            charge = Math.min(A,B);
            System.out.format("#%d %d\n",x,charge);
        }
    }
}

4 Python[ | ]

T = int(input())
for t in range(1, T+1):
    P, Q, R, S, W = map(int, input().split())

    cost_A = W * P 
    cost_B = Q
    if R < W:
        cost_B += S * (W-R)

    cost = min(cost_A, cost_B)

    print('#{} {}'.format(t, cost))
문서 댓글 ({{ doc_comments.length }})
{{ comment.name }} {{ comment.created | snstime }}