카테고리 없음

백준 2975. Transactions (JAVA)

joah.k 2023. 2. 13. 16:55
728x90

https://www.acmicpc.net/problem/2975

 

2975번: Transactions

Input consists of a number of lines, each representing a transaction. Each transaction consists of an integer representing the starting balance (between –200 and +10,000), the letter W or the letter D (Withdrawal or Deposit), followed by a second integer

www.acmicpc.net

 

Transactions 

 
시간 제한메모리 제한제출정답맞힌 사람정답 비율
1 초 128 MB 1490 844 797 57.587%

문제

Whenever somebody goes to an ATM to withdraw or deposit money, a calculation has to be done to keep the person's bank balance correct. Your task in this problem is to do such calculations. There is a bank rule that says that a customer may not have an overdraft of more than $200, so any withdrawal that would take the balance below –200 must be stopped. (A minus sign is used to indicate an overdraft, or negative balance).

입력

Input consists of a number of lines, each representing a transaction. Each transaction consists of an integer representing the starting balance (between –200 and +10,000), the letter W or the letter D (Withdrawal or Deposit), followed by a second integer representing the amount to be withdrawn or deposited (between 5 and 400). Input will be terminated by a line containing 0 W 0.

출력

Output consists of one line for each line of input showing the new balance for each valid transaction If a withdrawal would take the balance below -200, the output must be the words ‘Not allowed’.

예제 입력 1 복사

10000 W 10
-200 D 300
50 W 300
0 W 0

예제 출력 1 복사

9990
100
Not allowed
 

 

 


풀이 : 원래 가지고 있던 돈을 W(출금하느냐), D(입금하느냐) 에 따라 잔액을 출력하고,

만약 잔액이 -200 이하이면 Not allowed를 출력하는 문제 

 

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.StringTokenizer;

public class Main {
    // 백준 2975. Transactions
    public static void main(String[] args) throws IOException {
        BufferedReader br= new BufferedReader(new InputStreamReader(System.in));
        while (true){
            StringTokenizer st = new StringTokenizer(br.readLine());
            int startingBalance = Integer.parseInt(st.nextToken());
            String letter = st.nextToken();
            int money = Integer.parseInt(st.nextToken());
            if(startingBalance==0 && "W".equals(letter) && money==0) break;

            int result = 0;
            if("W".equals(letter)){
                result = startingBalance - money;
            } else if ("D".equals(letter)) {
                result = startingBalance + money;
            }
            System.out.println(result<-200? "Not allowed": result);
        }
    }
}
728x90