
1. 문제 풀이 아이디어
- 반복문을 통해 자리수를 하나씩 검사하여 문제를 해결할 수 있다.
2. 나의 정답 코드
using (StreamReader sr = new(Console.OpenStandardInput()))
using (StreamWriter sw = new(Console.OpenStandardOutput()))
{
string[] split = sr.ReadLine().Split();
int n = int.Parse(split[0]);
int d = int.Parse(split[1]);
int result = 0;
for (int i = 1; i <= n; i++)
{
int m = i;
while (m > 0)
{
if (m % 10 == d)
result++;
m /= 10;
}
}
sw.WriteLine(result);
}
3. 정리
- 1부터 n까지 반복하면서 각 숫자를 하나씩 확인한다.
- 각 숫자에서 d가 포함된 횟수를 세기 위해, 그 숫자를 10으로 나누면서 각 자리를 확인한다.
- 숫자 m이 0이 될 때까지 각 자릿수를 확인하고, d가 포함되면
result
를 증가시킨다.
- 마지막으로 결과를 출력한다.
Share article