※ 글쓴이는 취미로 코딩을 익혀보는 사람이라 정확하지 않은 내용을 담고 있을 수 있다 ※

 

이번에 볼 문제는 백준 6887번 문제인 Squares이다.
문제는 아래 링크를 확인하자.

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

 

6887번: Squares

Gigi likes to play with squares. She has a collection of equal-sized square tiles. Gigi wants to arrange some or all of her tiles on a table to form a solid square. What is the side length of the largest possible square that Gigi can build? For example, wh

www.acmicpc.net

주어지는 (음이 아닌) 정수 N의 (양의) 제곱근 이하인 가장 큰 정수를 찾는 문제이다.

 

N이 10,000 이하로 주어지므로, 답 변수를 0부터 시작해 그 다음 수의 제곱이 N을 넘지 않을 때까지 1씩 증가시켜나가는 것으로 문제를 충분히 해결할 수 있다. 이러한 알고리즘의 구현은 while문을 이용해 간단히 할 수 있다.

 

아래는 제출한 소스코드이다.

#include <iostream>
using namespace std;

int N;

int main() {
	ios::sync_with_stdio(0);
	cin.tie(0);

	cin >> N;
	int ans = 0;
	while ((ans + 1) * (ans + 1) <= N) ans++;

	cout << "The largest square has side length " << ans << '.';
}
728x90

'BOJ' 카테고리의 다른 글

[BOJ 26545 // C++] Mathematics  (0) 2022.12.19
[BOJ 26546 // C++] Reverse  (0) 2022.12.19
[BOJ 26592 // C++] Triangle Height  (0) 2022.12.19
[BOJ 1809 // GolfScript] Moo  (0) 2022.12.19
[BOJ 2377 // FreeBASIC] Pottery  (0) 2022.12.19

+ Recent posts