작성일 :

문제 링크

28940번 - Дневник Гравити Фолз

설명

페이지 크기, 글자 크기, 총 글자 수가 주어질 때 필요한 페이지 수를 구하는 문제입니다. 한 글자도 들어가지 않으면 -1을 출력합니다.


접근법

먼저 한 페이지에 가로로 들어가는 글자 수와 세로로 들어가는 줄 수를 정수 나눗셈으로 구합니다.

다음으로 한 페이지에 들어가는 총 글자 수를 계산합니다. 이 값이 0이면 -1을 출력하고, 아니면 올림 나눗셈으로 필요한 페이지 수를 계산합니다.


Code

C#

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

class Program {
  static void Main() {
    var p1 = Console.ReadLine()!.Split();
    var w = int.Parse(p1[0]);
    var h = int.Parse(p1[1]);
    var p2 = Console.ReadLine()!.Split();
    var n = int.Parse(p2[0]);
    var a = int.Parse(p2[1]);
    var b = int.Parse(p2[2]);

    var cols = w / a;
    var rows = h / b;
    var cap = cols * rows;
    if (cap == 0) {
      Console.WriteLine(-1);
    } else {
      var pages = (n + cap - 1) / cap;
      Console.WriteLine(pages);
    }
  }
}

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 w, h; cin >> w >> h;
  int n, a, b; cin >> n >> a >> b;

  int cols = w / a;
  int rows = h / b;
  long long cap = 1LL * cols * rows;
  if (cap == 0) {
    cout << -1 << "\n";
  } else {
    long long pages = (n + cap - 1) / cap;
    cout << pages << "\n";
  }

  return 0;
}