관리 메뉴

MOMONOTE

(JAVA) 백준 10250 ACM 호텔 본문

알고리즘, 코딩테스트/(JAVA)백준

(JAVA) 백준 10250 ACM 호텔

momo0503 2021. 6. 13. 16:40

 

내가 짠 코드

import java.util.*;

public class Main{
    public static void main(String[] args){
    
        Scanner sc = new Scanner(System.in);
        StringBuilder sb = new StringBuilder();
        int testCase = sc.nextInt();
        
        
        
        // 6   12   4     - > 401
        
        for(int i=1;i<=testCase;i++){
            
             int H = sc.nextInt();
             int W = sc.nextInt();
             int N = sc.nextInt();
        
            int countW = 1; //W랑 관련.
            int countH = 1; //H랑 관련.
            
            while(true){
                
                if(H>=N){
                    countH = N;
                    break;
                }else{
                   
                   countW = countW + N/4;   
                   countH = N%H;  
                   break;
                }
            }//while , while을 통해 몇층 몇호인지 구함.
            
             
            sb.append(countH);
            if(countW<10){
                sb.append(0); sb.append(countW);
            }else{
                sb.append(countW);
            }
                
            System.out.println(sb);
        }//for
      sc.close();
           
    }//main
}

- testCase를 입력받아 H ,W, N을 입력받아. 

- while 문의 로직으로 N번째의  층과 호수를 구하여 출력한다.

 

로직은 맞는거 같은데 시간초과가 나서 속상했다. 

for 문에 while 문을 두어 시간초과가 난거 같다.  ㅠ 

 

 

 

 

https://st-lab.tistory.com/77

 

[백준] 10250번 : ACM 호텔 - JAVA [자바]

https://www.acmicpc.net/problem/10250 10250번: ACM 호텔 문제 ACM 호텔 매니저 지우는 손님이 도착하는 대로 빈 방을 배정하고 있다. 고객 설문조사에 따르면 손님들은 호텔 정문으로부터 걸어서 가장 짧은

st-lab.tistory.com

 

이분의 블로그에서 엄청 간단한 코드를 발견하였다..

 

import java.util.*;

public class Main{
    public static void main(String[] args){
    
        Scanner sc = new Scanner(System.in);
        
        int testCase = sc.nextInt();
        
        
        
        // 6   12   4     - > 401
        
        for(int i=1;i<=testCase;i++){
            
             int H = sc.nextInt();
             int W = sc.nextInt();
             int N = sc.nextInt();
        
   
            if(N % H ==0){
                System.out.println((H*100) +(N/H));
            }else{
                System.out.println(((N%H)*100 + ((N/H) + 1)));
            }

            
        }//for
      sc.close();
           
    }//main
}

 

너무 간단하고 깔끔했다.. ! 

간단한 문제라 별 다른 설명은 필요 없을거 같다..

 

끝.

Comments