본문 바로가기

백준 문제풀이/Implementation

[C++] 백준 문제풀이 (Implementation) 11170번 0의 개수

 

 

https://www.acmicpc.net/problem/11170

 

11170번: 0의 개수

N부터 M까지의 수들을 종이에 적었을 때 종이에 적힌 0들을 세는 프로그램을 작성하라. 예를 들어, N, M이 각각 0, 10일 때 0을 세면 0에 하나, 10에 하나가 있으므로 답은 2이다.

www.acmicpc.net

 

 

 

//[C++] 백준 문제풀이 (Implementation)

#include <bits/stdc++.h>


int solve(int num){
    if (num == 0) return 1;
    int cnt = 0;
    while(num != 0){
        if (num % 10 == 0) ++cnt;
        num /= 10;
    }
    return cnt;
}


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

    int t;
    std::cin >> t;
    int n, m;

    while(t--){
        int sum = 0;
        std::cin >> n >> m;
        for (int i = n; i <= m; ++i){
            sum += solve(i);
        }
        std::cout << sum << "\n";
    }
    return 0;
}