작성일 :

문제 링크

24603번 - Scaling Recipe

설명

레시피의 원래 분량과 목표 분량이 주어질 때, 각 재료량을 비례 확대해 출력하는 문제입니다. 결과는 항상 정수로 나옵니다.


접근법

원래 분량이 x이고 목표 분량이 y이면, 각 재료량에 y를 곱하고 x로 나누면 됩니다. 곱셈을 먼저 하고 나눗셈을 나중에 해야 정수 연산에서 정확한 결과를 얻습니다.


Code

C#

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

class Program {
  static void Main() {
    var first = Console.ReadLine()!.Split();
    var n = long.Parse(first[0]);
    var x = long.Parse(first[1]);
    var y = long.Parse(first[2]);

    for (var i = 0L; i < n; i++) {
      var a = long.Parse(Console.ReadLine()!);
      Console.WriteLine(a * y / x);
    }
  }
}

C++

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

typedef long long ll;

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

  ll n, x, y; cin >> n >> x >> y;

  for (ll i = 0; i < n; i++) {
    ll a; cin >> a;
    cout << a * y / x << "\n";
  }

  return 0;
}