개발문제풀이
[ 백준 2577 ] 숫자의 개수 JAVA(자바) 문제풀이
까잉이
2022. 3. 24. 11:01
여기서 문제는
입력받은 int를 배열로 만들어서 갯수를 출력하는 것이 핵심이다.
풀이순서
1. 입력받은 숫자를 곱한 뒤 String으로 변환하여 담는다
2. 배열 선언을 할때 String으로 받은 숫자 자릿수를 length로 해서 배열갯수를 넣어준다
3. result2로 다시 string을 int로 형변환 시켜준다
4. 나눗셈 연산을 이용해서 자릿수를 배열에 담는다
5. for문을 이용해서 자릿수마다 0~9까지 비교해 카운팅한다
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int a = sc.nextInt();
int b = sc.nextInt();
int c = sc.nextInt();
String result = Integer.toString(a*b*c);
int i = 0;
int[] arr = new int[result.length()];
int result2 = Integer.parseInt(result);
// 나눗셈 연산을 이용해서 자릿수를 배열에 담는다
while(result2 > 0) {
arr[i] = result2 % 10;
result2 /= 10;
i++;
}
// 0~9까지 비교해서 count에 갯수를 담는다
for(int j = 0; j < 10; j++) {
int count = 0;
for(int k = 0; k < arr.length; k++) {
if(arr[k] == j) {
count += 1;
}
}
System.out.println(count);
}
}
}