PS

BOJ 5696 : 숫자 세기

lickelon 2024. 4. 28. 20:26
  • 문제 링크 : boj.kr/5696
  • 난이도 : G3
  • 태그 : 수학

코드

#include <bits/stdc++.h>

#define all(x) (x).begin(), (x).end()

#define INF 0x7FFFFFFF

using namespace std;

using ll = long long;
using ld = long double;
using pii = pair<int,int>;
using pll = pair<ll, ll>;

int check[10];

void calc(int n, int ten, vector<int> &arr)
{
	while (n > 0) {
		arr[n % 10] += ten;
		n /= 10;
	}
}

void solve(int A, int B, int ten, vector<int> &arr) {
	while (A % 10 != 0 && A <= B)
	{
		calc(A, ten, arr);
		A++;
	}
	if (A > B) return;
	while (B % 10 != 9 && B >= A)
	{
		calc(B, ten, arr);
		B--;
	}

	int cnt = (B / 10 - A / 10 + 1);
	for (int i = 0; i < 10; ++i)
		arr[i] += cnt * ten;

	solve(A / 10, B / 10, ten * 10, arr);
}

int main()
{
    ios::sync_with_stdio(0);
    cin.tie(0); cout.tie(0);

    while(true) {
        int a, b;
        cin >> a >> b;
        if(a == 0 && b == 0) break;
        vector<int> arr(10);
        solve(a, b, 1, arr);
        for(auto u : arr) cout << u << " ";
        cout << "\n";
    }

    return 0;
}

풀이

1019번과 사실상 같은 문제이다.

'PS' 카테고리의 다른 글

BOJ 26216 : 은나무  (0) 2024.04.30
BOJ 16398 : 행성 연결  (0) 2024.04.29
BOJ 28137 : 뭐라고? 안들려  (0) 2024.04.27
BOJ 2778 : 측량사 지윤  (0) 2024.04.26
BOJ 14204 : 표 정렬  (0) 2024.04.25