백준 9단계 - 약수, 배수, 소수

이상훈·2023년 3월 21일
0

5086번

import java.io.*;
import java.util.*;

public class Main {
    public static void main(String[] args) throws IOException {
        BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));
        StringTokenizer st;
        while (true) {
            st = new StringTokenizer(bf.readLine());
            int a = Integer.parseInt(st.nextToken());
            int b = Integer.parseInt(st.nextToken());
            
            if (a == 0 && b == 0) {
                break;
            } else if (b % a == 0) {
                System.out.println("factor");
            } else if (a % b == 0) {
                System.out.println("multiple");
            } else {
                System.out.println("neither");
            }
        }
    }
}

2501번

import java.util.*;
import java.io.*;

public class Main {
    public static void main(String[] args) throws IOException {
        BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));
        StringTokenizer st = new StringTokenizer(bf.readLine());
        int a = Integer.parseInt(st.nextToken());
        int b = Integer.parseInt(st.nextToken());
        
        int[] arr = new int[a+1];
        for (int i = 1; i<arr.length; i++) {
            if (a % i == 0) {
                arr[i] = i;
            } else {
                arr[i] = 0;
            } 
        }
        int[] arr2 = new int[a+1];
        int i= 1;
        for (int num : arr) {
            if (num != 0) {
                arr2[i] = num;
                i++;
            }
        }
        if (arr2[b] != 0) {
            System.out.println(arr2[b]);
        } else {
            System.out.println("0");
        }
    }
}

9506번

import java.io.*;
import java.util.*;

public class Main {
	public static void main(String[] args) throws IOException {
		BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));

		while (true) {
			int a = Integer.parseInt(bf.readLine());
			if (a == -1) {
				break;
			}
			int[] arr = new int[a+1];
			for (int i = 1; i<a+1; i++) {
				if (i == a) {
					arr[i] = 0;
				} else if (a % i == 0) {
					arr[i] = i;
				} else {
					arr[i] = 0;
				}
			}
			int sum = 0;
			for (int num : arr) {
				sum += num;
			}
			if (sum == a) {
				System.out.print(a+" = 1");
				for (int num : arr) {
					if (num >1 && num != 0) {
						System.out.print(" + "+num);
					}
				}
				System.out.println();
			} else {
				System.out.println(a+" is NOT perfect.");
			}
		}
	}
}

1978번

import java.io.*;
import java.util.*;
import javax.swing.plaf.IconUIResource;

public class Main {
	static int aaa(int a) {
		int cnt = 0;
		if (a == 1) {
			return cnt;
		} else if (a == 2 || a == 3) {
			cnt++;
			return cnt;
		}
		for (int j = 2; j<a-1; j++) {
			if (a % j == 0) {
				return cnt;
			}
		}
		cnt++;
		return cnt;
	}

	public static void main(String[] args) throws IOException {
		BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));
		StringTokenizer st;
		int num = Integer.parseInt(bf.readLine());

		st = new StringTokenizer(bf.readLine());
		int sum = 0;

		for (int i = 0; i<num; i++) {
			int a = Integer.parseInt(st.nextToken());

			sum+=aaa(a);
		}
		System.out.println(sum);
	}
}

2581번

import java.util.*;
import java.io.*;

public class Main {

	static int aaa(int a) {
		if (a == 1) {
			return 0;
		} else if (a == 2 || a == 3) {
			return a;
		}
		for (int j = 2; j<a-1; j++) {
			if (a % j == 0) {
				return 0;
			}
		}
		return a;
	}

	public static void main(String[] args) throws IOException {
		BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));
		int a = Integer.parseInt(bf.readLine());
		int b = Integer.parseInt(bf.readLine());

		int[] arr = new int[b+1];
		for (int i = a; i<=b; i++) {
			arr[i] = aaa(i);
		}
		int sum = 0;
		int min = 10001;
		for (int num : arr) {
			sum+=num;
			if (num !=0 && num < min) {
				min = num;
			}
		}

		if (sum == 0) {
			System.out.println("-1");
		} else {
			System.out.println(sum);
			System.out.println(min);
		}
	}
}

11653번

import java.util.*;
import java.io.*;

public class Main {

	// a는 소수인지 판별
	static int aaa(int a) {
		if (a == 1) {
			return 0;
		} else if (a == 2 || a == 3) {
			return a;
		}
		for (int j = 2; j<a-1; j++) {
			if (a % j == 0) {
				return 0;
			}
		}
		return a;
	}
	public static void main(String[] args) throws IOException {
		BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));
		int num = Integer.parseInt(bf.readLine());

		while (aaa(num) == 0) {
			if (num == 1) {
				System.out.println();
				break;
			} else {
				for (int i = 1; i<num; i++) {
					int a = aaa(i);
					if (a !=0 && num % a == 0) {
						System.out.println(a);
						num = num / a;
						break;
					}
				}
			}
		}
		if (num != 1) {
			System.out.println(num);
		}
	}
}

0개의 댓글