알고리즘 문제풀이/JAVA 알고리즘 문제풀이
백준(BaekJoon) 3052번 1차원 배열_나머지
코딩하는 백수몬
2020. 3. 25. 09:00
문제
https://www.acmicpc.net/problem/3052
3052번: 나머지
문제 두 자연수 A와 B가 있을 때, A%B는 A를 B로 나눈 나머지 이다. 예를 들어, 7, 14, 27, 38을 3으로 나눈 나머지는 1, 2, 0, 2이다. 수 10개를 입력받은 뒤, 이를 42로 나눈 나머지를 구한다. 그 다음 서��
www.acmicpc.net
package baekjoon_7;
import java.util.Scanner;
public class CountDivision {
public static void main(String[] args) {
// get 10 numbers
int num[] = new int[10];
Scanner input = new Scanner(System.in);
System.out.println("10개의 숫자를 입력하세요.");
for (int i = 0; i < num.length; i++) {
int n = 0;
n = input.nextInt();
while (n < 0 || n > 1000) {
System.out.println("숫자를 다시 입력하세요.");
n = input.nextInt();
}
num[i] = n;
}
// Calculate and put division in array num
int div = 42;
for (int i = 0; i < num.length; i++) {
num[i] = num[i] % div;
}
// Print divisions
// make a array to check position (0: not counted, 1: counted)
int position[] = new int[10];
for (int i = 0; i < position.length; i++) {
position[i] = 0;
}
//count the first number of array 'num'
int i = 0, j = 1, count = 1;
System.out.print(num[0] + " ");
position[0] = 1;
//count divisions
while (i < num.length) {
//check the position of the same number with num[i]
for (; j < num.length ; j++) {
if(position[j] == 0 && num[i] == num[j])
position[j] = 1;
}
//change i and j
while (i < position.length && position[i] == 1)
i++;
j = i + 1;
//print and count next division
if(i >= position.length) {
break;
}
else {
System.out.print(num[i] + " ");
count++;
position[i] = 1;
}
}
//print the number of different divisions
System.out.print("/ the number of different divisions is " + count);
}
}