[Programmers] #120891 - 369게임 [Java][C++][Python]
[Programmers] #120891 - 369게임 [Java][C++][Python]
1. 아이디어
머쓱이가 말해야 하는 숫자에 들어 있는 3, 6, 9의 개수가 곧 박수 횟수다. 1부터 하나씩 세어 올라갈 필요 없이 그 숫자 하나의 자릿수만 보면 된다.
2. 코드
풀이 [Java][C++][Python]
1
2
3
4
5
6
7
8
9
10
11
12
class Solution {
public int solution(int order) {
int cnt = 0;
while (order > 0) {
int d = order % 10;
if (d == 3 || d == 6 || d == 9) cnt++;
order /= 10;
}
return cnt;
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
#include <bits/stdc++.h>
using namespace std;
int solution(int order) {
int cnt = 0;
while (order > 0) {
int d = order % 10;
if (d == 3 || d == 6 || d == 9) cnt++;
order /= 10;
}
return cnt;
}
1
2
def solution(order):
return sum(c in "369" for c in str(order))
조건식이 참이면 1로 세어져 sum을 개수로 썼다.
This post is licensed under CC BY 4.0 by the author.