작성일 :

문제 링크

17174번 - 전체 계산 횟수

설명

지폐를 세며 묶음을 만들 때 전체 계산 횟수를 구하는 문제입니다.


접근법

처음 n장을 세는 횟수를 시작값으로 둡니다.

n을 m으로 나눈 몫을 계속 누적하며, 몫이 m보다 작아질 때까지 반복합니다.

누적된 값이 전체 계산 횟수가 됩니다.



Code

C#

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

class Program {
  static void Main() {
    var line = Array.ConvertAll(Console.ReadLine()!.Split(), int.Parse);
    var n = line[0]; var m = line[1];
    var total = n;
    while (n >= m) {
      n /= m;
      total += n;
    }
    Console.WriteLine(total);
  }
}

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;

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

  int n, m;
  if (!(cin >> n >> m)) return 0;
  int total = n;
  while (n >= m) {
    n /= m;
    total += n;
  }
  cout << total << "\n";

  return 0;
}