PS/백준
백준 (2292번: 벌집)_Java
codeStudy123
2021. 12. 25. 23:35
제목: 벌집
브론즈2
문제

위의 그림과 같이 육각형으로 이루어진 벌집이 있다. 그림에서 보는 바와 같이 중앙의 방 1부터 시작해서 이웃하는 방에 돌아가면서 1씩 증가하는 번호를 주소로 매길 수 있다. 숫자 N이 주어졌을 때, 벌집의 중앙 1에서 N번 방까지 최소 개수의 방을 지나서 갈 때 몇 개의 방을 지나가는지(시작과 끝을 포함하여)를 계산하는 프로그램을 작성하시오. 예를 들면, 13까지는 3개, 58까지는 5개를 지난다.
입력
첫째 줄에 N(1 ≤ N ≤ 1,000,000,000)이 주어진다.
출력
입력으로 주어진 방까지 최소 개수의 방을 지나서 갈 때 몇 개의 방을 지나는지 출력한다.
🔑 코드
타입은 두 가지를 가지고 왔다.
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
class Main{
public static void main(String args[]) throws IOException{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
int N=Integer.parseInt(br.readLine());
int cut=1;
int stop=1;
if(N==1) {
cut=1;
}else {
while(N>stop) {
stop=(cut*6)+stop;
cut++;
}
}
System.out.print(cut);
}
}
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
class Main{
public static void main(String args[])throws IOException{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
int N=Integer.parseInt(br.readLine());
int room=1;
int count=1;
if(N==1){
System.out.println(1);
}else {
for(int i=1;N>room;i++) {
room=(i*6)+room;
count++;
}
System.out.println(count);
}
}
}
한 개는 while을 이용해 방의 개수를 연산했고 다른 하나는 for문을 이용해 방의 개수를 연산했다.
알고리즘 분류
https://www.acmicpc.net/problem/2292
2292번: 벌집
위의 그림과 같이 육각형으로 이루어진 벌집이 있다. 그림에서 보는 바와 같이 중앙의 방 1부터 시작해서 이웃하는 방에 돌아가면서 1씩 증가하는 번호를 주소로 매길 수 있다. 숫자 N이 주어졌
www.acmicpc.net