본문 바로가기

Algorithm/Implementation

(C++) - 백준(BOJ) 15059 : Hard choice

반응형

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

 

15059번: Hard choice

The first line contains three integers Ca, Ba and Pa (0 ≤ Ca, Ba, Pa ≤ 100), representing respectively the number of meals available for chicken, beef and pasta. The second line contains three integers Cr, Br and Pr (0 ≤ Cr, Br, Pr ≤ 100), indicati

www.acmicpc.net

for문과 if문을 사용하는 문제였습니다.

 

📕 풀이방법

📔 입력 및 초기화

기내에서 가지고 있는 닭고기, 쇠고기, 파스타의 각 개수를 저장할 일차원 배열 a를 선언, 손님들이 주문한 메뉴의 각 개수를 저장할 일차원 배열 b를 선언 후 입력받습니다. 답을 출력할 변수 ans

 

📔 풀이과정

닭고기, 쇠고기, 파스타에 대해 loop를 돌며 b[i]가 더 크다면 b[i] - a[i]를 ans에 더합니다.

 

📔 정답출력

ans를 출력합니다.


📕 Code

#include <bits/stdc++.h>
using namespace std;
int a[3], b[3], ans; //닭고기, 쇠고기, 파스타
int main(){
    for(int i = 0; i < 3; i++) cin >> a[i];
    for(int i = 0; i < 3; i++) cin >> b[i];
    for(int i = 0; i < 3; i++) {
        if(a[i] < b[i]) ans += b[i] - a[i];
    }
    cout << ans;
}