728x90
SW Expert Academy
SW 프로그래밍 역량 강화에 도움이 되는 다양한 학습 컨텐츠를 확인하세요!
swexpertacademy.com
import java.util.Scanner;
class Solution {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int t = sc.nextInt();
for(int tc=1;tc<=t;tc++) {
long n = sc.nextLong();
long a=0,b=0;
for(long i=1;i<=Math.sqrt(n);i++) { //10^12의 제곱근은 int 범위 안이기 때문에 int로 해줘도 된다.
if(n%i==0) {
a=i;
b=n/i;
}
}
System.out.println("#"+tc+" "+(a+b-2)); //1,1에서 출발하기 때문에 오른쪽 한 번, 아래 쪽 한 번은 이미 갔다. -2
}
}
}
import java.util.Scanner;
class Solution {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int t = sc.nextInt();
for(int tc=1;tc<=t;tc++) {
long n = sc.nextLong();
long a=0,b=0;
if(isPrime(n)) {
a=1;
b=n;
}
else {
for(long i=1;i<=Math.sqrt(n);i++) {
if(n%i==0) {
a=i;
b=n/i;
}
}
}
System.out.println("#"+tc+" "+(a+b-2));
}
}
public static boolean isPrime(long n) {
for(long i=2;i<=Math.sqrt(n);i++) {
if(n%i==0) {
return false;
}
}
return true;
}
}
728x90
'SW Expert Academy > SWEA D3' 카테고리의 다른 글
| [SW Expert Academy] 기차 사이의 파리 (D3) (1) | 2024.09.28 |
|---|---|
| [SW Expert Academy] 주혁이의 복권 당첨 (D3) (0) | 2024.09.28 |
| [SW Expert Academy] 신뢰 (D3) (0) | 2024.09.28 |
| [SW Expert Academy] 세상의 모든 팰린드롬 2 (D3) (0) | 2024.09.27 |
| [SW Expert Academy] 유효기간 (D3) (0) | 2024.09.27 |