작성일 :

문제 링크

10156번 - 과자

설명

과자의 단가와 원하는 수량, 현재 가진 돈이 주어질 때 부족한 금액을 계산하는 간단한 조건 분기 문제입니다.

  • 단가와 구매하고자 하는 수량이 주어집니다.
  • 현재 가진 금액이 주어지고, 구매에 필요한 총 금액과 비교합니다.
  • 만약 가진 금액이 충분하다면 0을 출력하고,
    부족하다면 그 차이만큼의 금액을 출력합니다.

접근법

  • 입력으로 단가, 수량, 가진 돈을 차례대로 입력받습니다.
  • 단가 * 수량으로 총 필요한 금액을 계산하고, 가진 돈과 비교하여 조건 분기합니다.
  • 부족하지 않으면 0, 부족하면 필요 금액 - 가진 금액을 출력합니다.

Code

[ C# ]

1
2
3
4
5
6
7
8
9
10
11
12
13
using System;

class Program {
  static void Main() {
    var input = Console.ReadLine().Split();
    int cost = int.Parse(input[0]);
    int count = int.Parse(input[1]);
    int money = int.Parse(input[2]);

    int total = cost * count;
    Console.WriteLine(total <= money ? 0 : total - money);
  }
}



[ C++ ]

1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include <bits/stdc++.h>

using namespace std;

int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);

  int cost, cntS, deposit; cin >> cost >> cntS >> deposit;
  if (cntS * cost <= deposit) cout << "0\n";
  else cout << cntS * cost - deposit << "\n";

  return 0;
}