작성일 :

문제 링크

33963번 - 돈복사

설명

현재 금액을 두 배로 늘릴 때 자리수가 바뀌기 전까지 누를 수 있는 최대 횟수를 구하는 문제입니다.


접근법

현재 자리수에서 가장 큰 값은 9, 99, 999처럼 10의 제곱에서 1을 뺀 값입니다.
이 값보다 커지는 순간 자리수가 늘어나므로, 2배를 반복해도 이 한계를 넘지 않는 횟수를 세면 됩니다.


Code

C#

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

class Program {
  static void Main() {
    var n = long.Parse(Console.ReadLine()!);
    var limit = 1L;
    var temp = n;
    while (temp > 0) {
      limit *= 10;
      temp /= 10;
    }
    limit -= 1;

    var cnt = 0;
    while (n * 2 <= limit) {
      n *= 2;
      cnt++;
    }

    Console.WriteLine(cnt);
  }
}

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
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;

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

  ll n; cin >> n;
  ll limit = 1;
  ll temp = n;
  while (temp > 0) {
    limit *= 10;
    temp /= 10;
  }
  limit -= 1;

  int cnt = 0;
  while (n * 2 <= limit) {
    n *= 2;
    cnt++;
  }

  cout << cnt << "\n";

  return 0;
}