๋ฐ์ํ
import java.util.Scanner;
// 2292 ๋ฒ์ง
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int n = in.nextInt();
in.close();
int around = 1;
int cnt = 0;
for (int i = 0; i <= n; i++) {
if (i == around) {
cnt++;
if (i == 1) {
around = 2;
continue;
}
around = around + (6 * (cnt - 1));
}
}
System.out.println(cnt);
}
}
- ์ฒจ์ collection์ ์ด์ฉํด ๋ฒ์ง์ ๋ฐฐ์ด๋ก ๋ง๋ค์ด์ผ ํ๋ ์๊ฐํ๋๋ฐ
- ๊ฐฏ์๋ง ์ธ์ฃผ๋ฉด ๋๋๊น ๊ตณ์ด ๊ทธ๋ด ํ์๊ฐ ์๋ ๊ฑธ ๊นจ๋ฌ์์ต๋๋ค.
- aroud๊ฐ ๋ฒ์ง ํ๋ฐํด๋ก ์๊ฐํ๊ณ , ๋ฐฉ์ ์ง๋๊ฐ๋ ์ฒซ ์ซ์๊ฐ 1, 2, 8, 20, 38..
- ์ฌ๊ธฐ์ 1์ผ ๋๋ฅผ ์ ์ธํ๊ณ ๋ 6, 12, 18..์ด๋ฐ์์ผ๋ก ๋์ด๋๋ ๊ฑธ ํ์ธ ํ ์ ์์ต๋๋ค.
- ๊ทธ๋ผ ์ด์ ํ๋ฐํด์ ์ฒซ ์ซ์ + ( 6 * ์ง๋์จ ๋ฐฉ์ ๊ฐฏ์ - 1) ์ด ๊ทธ๋ค์ ๋ฐฉ์ ์ฒซ์ซ์๊ฐ ๋ฉ๋๋ค.
- ์ ๋ ฅ๋ ์ซ์ n๊น์ง for๋ฌธ์ ํตํด around๊ฐ ์ผ์นํ ๋๋ง cnt++; ํด์ฃผ์ด ๊ฒฐ๊ณผ๋ฅผ ์ถ๋ ฅํฉ๋๋ค.
๋ฐ์ํ
'๐ธ Algorithm > ๐ธ ๋ฐฑ์ค BaekJoon' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[๋ฐฑ์ค][java][1193][๋ถ์์ฐพ๊ธฐ] (0) | 2024.03.21 |
---|---|
[๋ฐฑ์ค][java][2903][์ค์ ์ด๋ ์๊ณ ๋ฆฌ์ฆ] (0) | 2024.03.14 |
[๋ฐฑ์ค][java][2720][์ธํ์ ์ฌ์ฅ ๋ํ] (0) | 2024.03.14 |
[๋ฐฑ์ค][java][11005][์ง๋ฒ ๋ณํ 2] (0) | 2024.03.13 |
[๋ฐฑ์ค][java][2745][์ง๋ฒ ๋ณํ] (3) | 2024.03.12 |