※ 글쓴이는 취미로 코딩을 익혀보는 사람이라 정확하지 않은 내용을 담고 있을 수 있다 ※
이번에 볼 문제는 백준 25841번 문제인 Digit Count이다.
문제는 아래 링크를 확인하자.
https://www.acmicpc.net/problem/25841
25841번: Digit Count
There is only one input line; it provides the range and the digit. Each integer for the range will be between 1000 and 9999 (inclusive) and the digit will be between 0 and 9 (inclusive). Assume the first integer for the range is not greater than the second
www.acmicpc.net
범위에 주어지는 각 수마다 to_string을 이용하여 주어진 수를 문자열화하고 각 문자의 개수를 계수해 문제를 해결하자.
또는 각 자릿수를 구하는 식을 작성해 문제를 해결할 수도 있다. 예를 들어 양의 정수 x의 100의 자리는 (x/100)%10 과 같이 구할 수 있다.
아래는 제출한 소스코드이다.
#include <iostream>
#include <string>
using namespace std;
int L, R; char dgt;
int cnt[128];
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
cin >> L >> R >> dgt;
for (int i = L; i <= R; i++) {
string s = to_string(i);
for (auto& l : s) cnt[l]++;
}
cout << cnt[dgt];
}
728x90
'BOJ' 카테고리의 다른 글
[BOJ 25871 // C++] Historical TV Remote Control (0) | 2022.11.08 |
---|---|
[BOJ 25904 // C++] 안녕 클레오파트라 세상에서 제일가는 포테이토칩 (0) | 2022.11.08 |
[BOJ 25840 // C++] Sharing Birthdays (0) | 2022.11.08 |
[BOJ 25638 // C++] 트리와 경로 개수 쿼리 (0) | 2022.11.07 |
[BOJ 25934 // C++] Brownies vs. Candies vs. Cookies (0) | 2022.11.06 |