본문 바로가기

Algorithm

[ 백준 / 1977 ] 완전제곱수 ( 자바 )

728x90
반응형

 

www.acmicpc.net/problem/1977

 

1977번: 완전제곱수

M과 N이 주어질 때 M이상 N이하의 자연수 중 완전제곱수인 것을 모두 골라 그 합을 구하고 그 중 최솟값을 찾는 프로그램을 작성하시오. 예를 들어 M=60, N=100인 경우 60이상 100이하의 자연수 중 완

www.acmicpc.net

케이스에 포함되는 모든 문자에 대해서 Math.pow()를 이용하면 쉽게 풀 수 있다.잊지말자, 제곱의 값은 Math.pow()!

 

 

//1977 완전제곱수
import java.util.*;
public class Main {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Scanner sc = new Scanner(System.in);
		
		int M = sc.nextInt();
		int N = sc.nextInt();
		
		List<Integer> list = new ArrayList<>();
		for(int i=1;i<=100;i++) {
			int val = (int)Math.pow(i,2);
			if(val<=N && val>=M) list.add(val);
			if(val>N) break;
		}
		
		if(list.size()==0) 
			System.out.println(-1);
		else {
			int sum = 0;
			for(int i=0;i<list.size();i++)
				sum += list.get(i);
			System.out.println(sum);
			System.out.println(list.get(0));
		}
	}

}

 

 

728x90
반응형