작성일 :

문제 링크

11006번 - 남욱이의 닭장

설명

닭의 수와 다리의 총합이 주어졌을 때, 두 다리를 가진 정상 닭과 한 다리만 있는 닭의 수를 구하는 문제입니다.

  • 다리의 합닭의 수가 주어집니다.
  • 모든 닭은 한 마리당 다리를 1개 또는 2개 가질 수 있습니다.
  • 두 가지 조건을 만족하는 닭의 수를 계산해야 합니다:
    • 총 닭 수 = 두 다리 닭 수 + 한 다리 닭 수
    • 총 다리 수 = 2 * 두 다리 닭 수 + 1 * 한 다리 닭 수

접근법

  1. 입력으로 주어지는 다리의 수와 닭의 수를 변수로 저장합니다.
  2. 두 변수에 대해 다음 수식을 유도합니다:
    • 한 다리 닭 수 : \(2 \times \text{닭 수} - \text{다리 수}\)
    • 두 다리 닭 수 : \(\text{닭 수} - \text{한 다리 닭 수}\)
  3. 위 수식을 그대로 계산하여 출력합니다.
  • O(1) 계산만 수행됩니다.

Code

C#

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

class Program {
  static void Main() {
    int t = int.Parse(Console.ReadLine());
    while (t-- > 0) {
      var input = Console.ReadLine().Split();
      int legSum = int.Parse(input[0]);
      int chicken = int.Parse(input[1]);

      int oddLeg = 2 * chicken - legSum;
      Console.WriteLine($"{oddLeg} {chicken - oddLeg}");
    }
  }
}

C++

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

using namespace std;

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

  int t; cin >> t;
  while (t--) {
    int legSum, cntChicken; cin >> legSum >> cntChicken;
    int oddLeg = 2 * cntChicken - legSum;
    cout << oddLeg << " " << cntChicken - oddLeg << "\n";
  }

  return 0;
}