[백준 17614] 369 (C#, C++) - soo:bak
작성일 :
문제 링크
설명
1부터 N까지 369 게임을 진행할 때, 총 박수 횟수를 구하는 문제입니다.
접근법
각 숫자의 자릿수를 확인하면서 3, 6, 9가 나올 때마다 박수 횟수를 늘립니다.
자릿수는 10으로 나누면서 나머지를 확인하는 방식으로 분해합니다.
1부터 N까지 모든 수에 대해 이 과정을 반복하면 됩니다.
Code
C#
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
using System;
class Program {
static void Main() {
var n = int.Parse(Console.ReadLine()!);
var cnt = 0;
for (var x = 1; x <= n; x++) {
var t = x;
while (t > 0) {
var d = t % 10;
if (d == 3 || d == 6 || d == 9) cnt++;
t /= 10;
}
}
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
#include <bits/stdc++.h>
using namespace std;
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int n; cin >> n;
int cnt = 0;
for (int x = 1; x <= n; x++) {
int t = x;
while (t) {
int d = t % 10;
if (d == 3 || d == 6 || d == 9) ++cnt;
t /= 10;
}
}
cout << cnt << "\n";
return 0;
}