‡ CODING TEST STUDY ‡/º 백준

[백준 | Java Bronze III ] (#15633) Fan Death

Trudy | 송연 2024. 7. 7. 02:28

 문제

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

 


첫 코드 - 성공, 비효율적

package week10.baek.july9;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class B15633 {
    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        int n = Integer.parseInt(br.readLine());

        int count = 0;
        for (int i = 1; i <= n; i++) {
            if(n%i == 0) {
                count += i;
            }
        }
        count = ( count ) * 5 - 24;

        System.out.println(count);

    }
}

 

실패 코드 - 제곱근까지만 검사하는 약수 구하기 방법 사용
아니 왜 제곱근으로 풀려고 하면 실패가 뜨지 진짜로

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class Main {
    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        int n = Integer.parseInt(br.readLine());

        int count = 0;
        for (int i = 1; i * i <= n; i++) {
            if (n % i == 0) {
                count += i;
                if (i != n / i) {
                    count += n / i;
                }
            }
        }
        count = count * 5 - 24;  // (count + n) 에서 n을 더하지 않습니다

        System.out.println(count);
    }
}