알고리즘/문제풀이

2292 - 벌집(백준)

WhNi 2024. 5. 31. 14:07

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

#include <iostream>
#include <vector>
#include <cmath>

using namespace std;

int main()
{
	int n;
	int count = 1;
	int rooms = 1;
	cin >> n;

	// n = 1일때
	if (n == 1)
	{
		cout << count << "\n";
		return 0;
	}

	// n이 rooms 보다 작을때 
	while (rooms < n)
	{
		// 룸은 count *6 만큼 추가되고 
		rooms += 6 * count;
		// 추가된 횟수가 육각형의 테두리 한 라인, 1은 이미 포함하고 있음
		count++;
	}

	cout << count << "\n";
	return 0;
};

처음에 n을 가지고 빼면서 했는데, 중간중간 걸쳐지는 라인에서 자꾸 문제가 생겨서 그냥 새로운 데이터(Rooms)에 담아서 했더니 풀렸다.