728x90
문제
자연수가 들어 있는 배열에 3의 배수와 5의 배수 중 어떤 수가 더 많은지 구하기
- 3의 배수와 5의 배수를 각각 세줍니다.
- 3의 배수와 5의 배수의 개수를 비교해 줍니다.
- 만약 3의 배수가 더 많다면 "three"를 return 합니다.
- 만약 5의 배수가 더 많다면 "five"를 return 합니다.
- 만약 3과 5의 배수의 개수가 같다면 "same"을 return 합니다.
주어진 소스 코드
import java.util.*;
class Solution {
public int func_a(int[] arr) {
int count = 0;
return count;
}
public String func_b(int three, int five) {
}
public int func_c(int[] arr) {
int count = 0;
return count;
}
public String solution(int[] arr) {
int countThree = func_c(arr);
int countFive = func_a(arr);
String answer = func_b(countThree, countFive);
return answer;
}
}
public class Test_02 {
public static void main(String[] args) {
Solution sol = new Solution();
int[] arr = {2, 3, 6, 9, 12, 15, 10, 20, 22, 25};
String ret = sol.solution(arr);
System.out.println(ret);
}
}
예시
arr | return |
[2, 3, 6, 9, 12, 15, 10, 20, 22, 25] | "three" |
3의 배수 : 3, 6, 9, 12, 15 (총 5개)
5의 배수 : 10, 15, 20, 25 (총 4개)
소스 코드
import java.util.*;
class Solution {
public int func_a(int[] arr) {
int count = 0;
for(int i = 0; i < arr.length; i++) {
if(arr[i] % 5 == 0)
count += 1;
}
return count;
}
public String func_b(int three, int five) {
if(three > five) return "three";
else if(three < five) return "five";
else return "same";
}
public int func_c(int[] arr) {
int count = 0;
for(int i = 0; i < arr.length; i++) {
if(arr[i] % 3 == 0)
count += 1;
}
return count;
}
public String solution(int[] arr) {
int countThree = func_c(arr);
int countFive = func_a(arr);
String answer = func_b(countThree, countFive);
return answer;
}
}
public class Test_02 {
public static void main(String[] args) {
Solution sol = new Solution();
int[] arr = {2, 3, 6, 9, 12, 15, 10, 20, 22, 25};
String ret = sol.solution(arr);
System.out.println(ret);
}
}
소스 코드 풀이
public int func_a(int[] arr) {
int count = 0;
for(int i = 0; i < arr.length; i++) {
if(arr[i] % 5 == 0)
count += 1;
}
return count;
}
public int func_c(int[] arr) {
int count = 0;
for(int i = 0; i < arr.length; i++) {
if(arr[i] % 3 == 0)
count += 1;
}
return count;
}
func_a와 func_c 각각의 메소드에는 5의 배수와 3의 배수를 구별하고, 만약 해당하면 count값을 증가시켜줘야 하기에 for문 안에 각각의 배수임을 구별하는 if문을 작성해 준다.
public String func_b(int three, int five) {
if(three > five) return "three";
else if(three < five) return "five";
else return "same";
}
func_b 메소드에는 int three와 int five는 countThree와 countFive의 값을 넣고 비교해줘서 three가 더 크면 "three"를 return 해주고, five가 더 크다면 "five"를 return 해주고, 둘이 같다면 "same"을 return 해준다.
public String solution(int[] arr) {
int countThree = func_c(arr);
int countFive = func_a(arr);
String answer = func_b(countThree, countFive);
return answer;
}
solution 메소드에선 각각 return받은 값을 넣어주고, answer에는 출력 될 return문을 받는다.
public static void main(String[] args) {
Solution sol = new Solution();
int[] arr = {2, 3, 6, 9, 12, 15, 10, 20, 22, 25};
String ret = sol.solution(arr);
System.out.println(ret);
}
main에선 arr에 2, 3, 6, 9, 12, 15, 10, 20, 22, 25를 받아서 3의 배수는 3, 6, 9, 12, 15로 총 5개, 5의 배수는 10, 15, 20, 25로 총 4개라서 실행하여 출력되는 결과는 "three"이다.
실행 결과
three
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급 369 게임 (0) | 2023.03.24 |