C#/C# (백준)

[C#] 백준 알고리즘 2884번, 오븐 시계

서니션 2022. 7. 21. 11:12
728x90
반응형

알람 시계는 빼준 것이였다면 오븐 시계는 더해주는 형식. while문을 추가하여 작성하였다.

 

1. 처음 작성한 코드 ..

using System;

public class bj2525
{
    public static void Main(string[] args)
    {
        string[] htime = Console.ReadLine().Split();
        
        int h = int.Parse(htime[0]);
        int m = int.Parse(htime[1]);
        int c = int.Parse(Console.ReadLine());
        
        m+=c;
        
        if (m>60){
            h+=1;
            m-=60;
            if (h>23){
                h=0;
            }
        }
        Console.WriteLine($"{h} {m}");
    }
}

 

2. 답안 코드

using System;

public class bj2525
{
    public static void Main(string[] args)
    {
        string[] htime = Console.ReadLine().Split();
        
        int h = int.Parse(htime[0]);
        int m = int.Parse(htime[1]);
        int c = int.Parse(Console.ReadLine());
        
        m+=c;
        
        if (m>=60){
            while(m>=60){
                m-=60;
                h++;
                if (h>=24){
                    h=0;
                }
            }
        }
        Console.WriteLine($"{h} {m}");
    }
}
분에 해당하는 값이 60을 넘어가면 -60을 해주는 반복문을 이용하고 시에 해당하는 값이 24를 넘어가면 -24를 해준다.

 

728x90
반응형