슈콩

[SWEA] 구구단 걷기 본문

Algorithms/SWEA

[SWEA] 구구단 걷기

shukong 2025. 11. 15. 23:29

 

 

[문제]

https://swexpertacademy.com/main/code/problem/problemDetail.do?contestProbId=AYaf9W8afyMDFAQ9

 

SW Expert Academy

SW 프로그래밍 역량 강화에 도움이 되는 다양한 학습 컨텐츠를 확인하세요!

swexpertacademy.com

 

 

 

[소스 코드]

import java.io.*;
import java.util.*;
public class Solution {
	public static void main(String[] args) throws IOException {
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		int T = Integer.parseInt(br.readLine());
		for(int tc=1;tc<=T;tc++) {
			long n = Long.parseLong(br.readLine());
			if(!isPrime(n)) {
				System.out.println("#"+tc+" "+(n-1));
			}
			else {
				for(int i=(int)Math.sqrt(n);i>=1;i--) {
					if(n%i==0) {
						System.out.println("#"+tc+" "+((n/i+i)-2));
						break;
					}
				}
			}
		}
	}
	private static boolean isPrime(long n) {
		for(int i=2;i<=(int)Math.sqrt(n);i++) {
			if(n%i==0) {
				return true;
			}
		}
		return false;
	}
}

'Algorithms > SWEA' 카테고리의 다른 글

[SWEA] 두 전구  (0) 2025.11.15
[SWEA] 제곱수 만들기  (0) 2025.11.15
[SWEA] 공과 잡초  (0) 2025.11.15
[SWEA] 코딩 토너먼트1  (0) 2025.11.15
[SWEA] 영준이의 카드 카운팅  (0) 2025.11.15