Java/방과후 Cos pro

[Java] cos pro 2급 더 많은 배수 구하기

U__q 2023. 4. 4. 21:38
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_afunc_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 threeint fivecountThreecountFive의 값을 넣고 비교해줘서 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