작성일 :

문제 링크

26360번 - Basket

설명

슛 성공 여부와 반칙 여부에 따라 추가 자유투를 수행하고 총 득점을 계산하는 문제입니다.


접근법

기본 슛이 성공하면 해당 점수를 더하고, 반칙이 있으면 추가 자유투를 수행합니다.

자유투 개수는 기본 슛 성공 시 1개, 실패 시 기본 점수만큼이며, 성공한 자유투 수를 더해 총점을 구합니다.


Code

C#

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
using System;

class Program {
  static void Main() {
    var x = int.Parse(Console.ReadLine()!);
    var hit = int.Parse(Console.ReadLine()!);
    var foul = int.Parse(Console.ReadLine()!);

    var score = hit == 1 ? x : 0;
    if (foul == 1) {
      var shots = hit == 1 ? 1 : x;
      for (var i = 0; i < shots; i++) {
        var v = int.Parse(Console.ReadLine()!);
        score += v;
      }
    }

    Console.WriteLine(score);
  }
}

C++

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <bits/stdc++.h>
using namespace std;

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

  int x, hit, foul; cin >> x >> hit >> foul;

  int score = hit ? x : 0;
  if (foul == 1) {
    int shots = hit ? 1 : x;
    for (int i = 0; i < shots; i++) {
      int v; cin >> v;
      score += v;
    }
  }

  cout << score << "\n";

  return 0;
}