알람 시계는 빼준 것이였다면 오븐 시계는 더해주는 형식. 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를 해준다.
'C# > C# (백준)' 카테고리의 다른 글
[C#] 백준 알고리즘 2739번, 구구단 (0) | 2022.07.25 |
---|---|
[C#] 백준 알고리즘 2480, 주사위 세개 (0) | 2022.07.22 |
[C#] 백준 알고리즘 2884번, 알람 시계 (0) | 2022.07.20 |
[C#] 백준 알고리즘 14681번, 사분면 고르기 (0) | 2022.07.19 |
[C#] 백준 알고리즘 9498번, 시험 성적 (0) | 2022.07.18 |