Programming/Baekjoon

백준 1977 완전제곱수 [c++]

fishersheep 2022. 1. 13. 02:43
반응형
#include <iostream>
#include <cmath>
using namespace std;

int main()
{
	ios::sync_with_stdio(false);
	cin.tie(NULL);
	cout.tie(NULL);

	int temp;	//제곱근을 넣을 변수
	int m, n;	//입력받을 변수
	int first;	//가장작은 완전제곱수
	int count=0;	
	int sum=0;	//합계

	cin >> m;	//변수입력
	cin >> n;

	for (int i = m; i <= n; i++)	//입력한 m이상 n이하의 수 중에서 완전제곱수를 찾을 반복문
	{	
		temp = sqrt(i);	//i의 제곱근
		if (i == temp*temp)	//i가 제곱근*2일 경우
		{
			count++;
			sum += i;	
			if (count == 1)	//첫번째 완전제곱수
				first = i;
		}	
	}

	if (count == 0)	//0일 경우에 -1일 출력
		cout << "-1";
	else	//그외의경우에 합계와 최솟값 출력
	{
		cout << sum << '\n';
		cout << first;
	}
	return 0;
}
반응형

'Programming > Baekjoon' 카테고리의 다른 글

백준 5635 생일 [c++]  (0) 2022.01.13
백준 11098 첼시를도와줘 [c++]  (0) 2022.01.13
백준 2738 행렬덧셈 [c++]  (0) 2022.01.12
백준 1157 단어공부 [c++]  (0) 2022.01.11
백준 10951 A+B [c++]  (0) 2022.01.10