[BOJ / C++ ] #2992 ๋ฒŒ์ง‘

Inryuยท2020๋…„ 8์›” 13์ผ
0

Problem Solving

๋ชฉ๋ก ๋ณด๊ธฐ
3/51
post-thumbnail

๐Ÿ๋ฌธ์ œ

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

๐Ÿํ’€์ด ๊ณผ์ •

์ค‘์•™์˜ 1์—์„œ๋ถ€ํ„ฐ, ๋–จ์–ด์ง„ ๊ฑฐ๋ฆฌ์— ๋”ฐ๋ผ

๋–จ์–ด์ง„ ๊ฑฐ๋ฆฌ 1 = layer 1

๋–จ์–ด์ง„ ๊ฑฐ๋ฆฌ 2= layer 2

..

๋–จ์–ด์ง„ ๊ฑฐ๋ฆฌ5 = layer5 ๋ผ๊ณ  ๋‘”๋‹ค.

์ด๋•Œ, ๋ฒŒ์ง‘ ๋ฐฉ์˜ ์ˆซ์ž๋ฅผ N์ด๋ผ ํ•˜๋ฉด

layerN๋ฐฉ์˜ ๊ฐœ์ˆ˜
111
22~76
38~1912
420~3718
538~6124

์ดํ›„ ์ƒ๋žต

์ด์ œ ์ฃผ์–ด์ง€๋Š” N์ด ์–ด๋Š ๋ฒ”์œ„์— ์†ํ•˜๋Š”์ง€ ์•Œ์•„์•ผ ํ•œ๋‹ค.

๊ฐ ๋ ˆ์ด์–ด์—์„œ, ์ตœ๋Œ€ ๋ฒ”์œ„๋Š” 1, 7, 19, 37, 61 ๋กœ ๊ฐ๊ฐ 6, 12, 18, 24์”ฉ 6์˜ ๋ฐฐ์ˆ˜๋กœ ํ•ญ์ด ์ฆ๊ฐ€ํ•œ๋‹ค.

๊ทธ๋Ÿฌ๋ฏ€๋กœ while๋ฌธ ์•ˆ์—์„œ

  1. N์ด ์–ด๋–ค ๋ ˆ์ด์–ด์˜ ์ตœ๋Œ€ ๋ฒ”์œ„ (1, 7, 19, 37.. . ๋“ฑ)๋ณด๋‹ค ์ž‘์€์ง€ ๊ฒ€์‚ฌํ•˜๊ณ ,
  2. ์ž‘๋‹ค๋ฉด ๊ทธ ๋ ˆ์ด์–ด์— ์†ํ•œ ๊ฒƒ์ด๋ฏ€๋กœ ํ•ด๋‹น ๋ ˆ์ด์–ด๋ฅผ ์ถœ๋ ฅํ•œ๋‹ค.
  3. N์ด ์–ด๋–ค ๋ ˆ์ด์–ด์˜ ์ตœ๋Œ€ ๋ฒ”์œ„๋ณด๋‹ค ํฌ๋‹ค๋ฉด, ๋ ˆ์ด์–ด๋ฅผ ํ•˜๋‚˜ ์ฆ๊ฐ€์‹œํ‚ค๊ณ  ๊ทธ์— ๋”ฐ๋ผ ์ตœ๋Œ€ ๋ฒ”์œ„๋„ 6์˜ ๋ฐฐ์ˆ˜๋กœ ์ฆ๊ฐ€์‹œํ‚จ๋‹ค. 1.~3. ์˜ ๊ณผ์ •์„ ๋ฐ˜๋ณตํ•˜์—ฌ์•ผํ•œ๋‹ค.

๐Ÿ์ฝ”๋“œ

//#2992 ๋ฒŒ์ง‘
#include <iostream>
using namespace std;

int main(void) {

	//์ž…์ถœ๋ ฅ ์†๋„ ํ–ฅ์ƒ 
	cin.tie(NULL);
	cout.tie(NULL);
	ios_base::sync_with_stdio(false);
	
	int N;
	int layer = 1; // ํ˜„์žฌ ๋ ˆ์ด์–ด
	int layermax = 1; //ํ˜„์žฌ ๋ ˆ์ด์–ด์˜ ์ตœ๋Œ€ ๋ฒ”์œ„
	int tmp = 1; // 6*(tmp)๋ฅผ ํ•˜์—ฌ tmp๋ฅผ ๋Š˜๋ ค๊ฐ€๋ฉฐ 6์˜ ๋ฐฐ์ˆ˜๋ฅผ ๊ตฌํ•จ

	cin >> N;

	while (1) {

		// ํ˜„์žฌ ๋ ˆ์ด์–ด์˜ ์ตœ๋Œ€ ๋ฒ”์œ„๋ณด๋‹ค N์ด ์ž‘์€์ง€ ๊ฒ€์‚ฌ
		if (layermax >= N) {
			cout << layer; // ์ž‘๋‹ค๋ฉด ํ˜„์žฌ ๋ ˆ์ด์–ด์— ์†ํ•œ ๊ฒƒ์ด๋ฏ€๋กœ ์ถœ๋ ฅ
			break;
		}

		// N์ด ํ˜„์žฌ ๋ ˆ์ด์–ด์˜ ์ตœ๋Œ€ ๋ฒ”์œ„๋ณด๋‹ค ํฌ๋‹ค๋ฉด ๋ ˆ์ด์–ด๋ฅผ ์ฆ๊ฐ€
		layer++;
		layermax += 6 * (tmp); // ๋ ˆ์ด์–ด์˜ ์ตœ๋Œ€ ๋ฒ”์œ„๋„ 6์˜ ๋ฐฐ์ˆ˜๋กœ ์ฆ๊ฐ€
		tmp++;
		

	}

}
profile
๐Ÿ‘ฉ๐Ÿปโ€๐Ÿ’ป

0๊ฐœ์˜ ๋Œ“๊ธ€