728x90
문제
369게임은 여러 명이 같이하는 게임입니다. 게임의 규칙은 아래와 같습니다.
- 1부터 시작
- 한 사람씩 차례대로 숫자를 1씩 더해가며 말합니다.
- 말해야 하는 숫자에 3, 6, 9 중 하나라도 포함되어 있다면 숫자를 말하는 대신 숫자에 도함된 3, 6, 9 개수만큼 손뼉을 칩니다.
주어진 소스 코드
import java.util.*;
class Solution {
public int solution(int number) {
int count = 0;
for(int i = 1; i <= ; i++) {
int current = i;
int temp = ;
while(current != 0) {
if( ;) {
count ++;
System.out.print("pair");
}
current ;
}
if(temp == count)
System.out.print(i);
System.out.print("");
}
System.out.println();
return count;
}
}
public class Test_06 {
public static void main(String[] args) {
Solution sol = new Solution();
int number = 40;
int ret = sol.solution(number);
System.out.println(ret);
}
}
예시
number | return |
40 | 22 |
3, 6, 9 : 각 한 번씩 (+3)
13, 16, 19 : 각 한 번씩 (+3)
23, 26, 29 : 각 한 번씩 (+3)
30~39 : 십의 자리 열 번 + 일의 자리 세 번 (+13)
3 + 3 + 3 + 13 = 22번
소스 코드
import java.util.*;
class Solution {
public int solution(int number) {
int count = 0;
for(int i = 1; i <= number; i++) {
int current = i;
int temp = count;
while(current != 0) {
if(current % 10 == 3 || current % 10 == 6 || current % 10 == 9) {
count++;
System.out.println("pair");
} // if (while)
current /= 10;
} // while
if(temp == count)
System.out.print(i+" ");
//System.out.print(" ");
} // for
System.out.println();
return count;
} // solution
} // class
public class Test_06 {
public static void main(String[] args) {
Solution sol = new Solution();
int number = 40;
int ret = sol.solution(number);
System.out.println(ret);
}
}
소스 코드 풀이
int number의 값이 40이라 박수 횟수가 22번이 되어야한다.
3, 6, 9는 각 한 번씩 박수를 쳐서 count 값이 +3 증가하고, 13, 16, 19도 마찬가지로 한 번씩 박수를 쳐서 count 값이 +3 증가 (현재 count 값 : 6), 23, 26, 29까지 각 한 번씩 박수를 쳐서 count 값이 +3 증가 (현재 count 값 : 9), 30부터 10의 자리 숫자가 3이기에 계속 +1씩 증가해주며, 33, 36, 39 각 한 번씩 더 증가를 해서 총 +13 증가를 해준다.
3 + 3 + 3 + 13 = 22 이기에 return 값은 22가 출력이 된다.
실행 결과
1 2 pair
4 5 pair
7 8 pair
10 11 12 pair
14 15 pair
17 18 pair
20 21 22 pair
24 25 pair
27 28 pair
pair
pair
pair
pair
pair
pair
pair
pair
pair
pair
pair
pair
pair
40
22
728x90
'Java > 방과후 Cos pro' 카테고리의 다른 글
[Java] cos pro 2급 TV를 2대 이상 트는 총 시간 구하기 (0) | 2023.05.16 |
---|---|
[Java] cos pro 2급 소수의 개수 구하기 (0) | 2023.04.07 |
[Java] cos pro 2급 서로 다른 두 자연수 사이에서 짝수들의 제곱의 합 더하기 (0) | 2023.04.05 |
[Java] cos pro 2급 더 많은 배수 구하기 (0) | 2023.04.04 |