작성일 :

문제 링크

6436번 - Floppies

설명

SHAR 파일 크기가 주어질 때, 압축과 uuencode 변환을 거친 후 필요한 플로피 개수를 구하는 문제입니다.

압축하면 크기가 절반(반올림)이 되고, uuencode하면 1.5배(반올림)가 됩니다. 한 블록은 30,000줄이며 한 줄은 62바이트이므로 블록당 1,860,000바이트입니다.


접근법

먼저, 원본 크기를 압축하여 절반으로 줄입니다. 반올림은 정수 연산으로 (크기 + 1) / 2로 처리합니다.

다음으로, 압축된 크기를 1.5배하여 인코딩된 크기를 구합니다. 이 역시 반올림이 필요하므로 압축 크기에 (압축 크기 + 1) / 2를 더합니다.

이후, 인코딩된 크기를 블록 크기(1,860,000바이트)로 나누어 올림하면 필요한 플로피 개수가 됩니다.


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
24
25
26
27
28
29
30
using System;

namespace Solution {
  class Program {
    const int BLOCK = 62 * 30000;

    static void Main(string[] args) {
      var tc = 1;
      while (true) {
        var line = Console.ReadLine();
        if (line == null)
          break;
        var s = long.Parse(line);
        if (s == 0)
          break;

        var compressed = (s + 1) / 2;
        var half = (compressed + 1) / 2;
        var encoded = compressed + half;
        var floppies = (encoded + BLOCK - 1) / BLOCK;

        if (tc > 1)
          Console.WriteLine();
        Console.WriteLine($"File #{tc}");
        Console.WriteLine($"John needs {floppies} floppies.");
        tc++;
      }
    }
  }
}

C++

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

typedef long long ll;

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

  const ll BLOCK = 62LL * 30000;
  ll s;
  int tc = 1;
  while (cin >> s) {
    if (s == 0)
      break;
    ll compressed = (s + 1) / 2;
    ll half = (compressed + 1) / 2;
    ll encoded = compressed + half;
    ll floppies = (encoded + BLOCK - 1) / BLOCK;

    if (tc > 1)
      cout << "\n";
    cout << "File #" << tc << "\n";
    cout << "John needs " << floppies << " floppies.\n";
    tc++;
  }

  return 0;
}