백준 Baekjoon 13225번 Divisors - JAVA

Jaeho Kim·2022년 4월 22일
0

코딩테스트

목록 보기
85/110

https://www.acmicpc.net/problem/13225

문제
Given an integer n, compute the number of divisors of n.
A divisor is an integer, d (1 <= d <= n) that evenly divides n.
Example: If n=10, divisors are: 1, 2, 5 and 10. So the result would be 4.
Example: If n=104717, divisors are 1 and 104717. This is a prime number so the number of divisors is 2.

입력**
The first line contains an integer C (1 <= C <= 10) with the amount of numbers you need to process. The next C lines will contain an integer n (1 <= n < 10000) each. You have to compute the number of divisors of these values.

출력
For each integer n, print a line with the number n itself, a space and the number of divisors.

예제 입력 1

10
1
2
3
4
5
9999
31
10
20
1047

예제 출력 1

1 1
2 2
3 2
4 3
5 2
9999 12
31 2
10 4
20 6
1047 4
import java.io.IOException;
import java.util.Scanner;

public class Main {

	public static void main(String[] args) throws IOException {
		Scanner sc = new Scanner(System.in);

		int[] arr = new int[sc.nextInt()];

		for (int i = 0; i < arr.length; i++) {
			int num = sc.nextInt();
			System.out.println(num + " " + divisorCount(num));
		}

	}

	public static int divisorCount(int num) {
		int count = 0;
		int i = 1;
		while (i <= num) {
			if (num % i == 0)
				count += 1;
			i++;
		}
		return count;
	}

}
  • 설명
profile
Hello, World!

0개의 댓글