백준 온라인 코딩 문제풀이
Baekjoon Online Judge
Baekjoon Online Judge 프로그래밍 문제를 풀고 온라인으로 채점받을 수 있는 곳입니다.
www.acmicpc.net
코드 참고
wny0320 (박 상운)
Run code live in your browser. Write and run code in 50+ languages online with Replit, a powerful IDE, compiler, & interpreter.
replit.com
1330, 9498, 2753, 14681, 2884, 2525, 2480 문제 중 일부 문제에 대한 풀이
이번 문제들 중 제일 신경 쓴 2525와 2480에 대해 다룸
using System;
class Program
{
public static void Main(string[] args)
{
string[] time = Console.ReadLine().Split();
int[] time_int = new int[time.Length];
for (int index = 0; index < time.Length; index++)
{
time_int[index] = Int32.Parse(time[index]);
}
int cook_time = Int32.Parse(Console.ReadLine());
time_int[1] += cook_time;
while (time_int[1] >= 60)
{
time_int[0] += 1;
time_int[1] -= 60;
}
while (time_int[0] >= 24)
{
time_int[0] -= 24;
}
Console.Write(time_int[0]);
Console.Write(" ");
Console.Write(time_int[1]);
}
}
2525 문제는 요리 시간이 분으로 주어지는데 2884와 다르게 시간이 45분 고정이 아님
따라서 예를 들어 23시 55분에 요리 시작, 요리시간 24 * 60 + 10 이런식으로 주어질 수 있다고 가정
시간 계산을 반복문 안에 넣어 처리
using System;
using System.Collections.Generic;
using System.Linq;
class Program
{
public static void Main(string[] args)
{
string[] dice = Console.ReadLine().Split();
int[] dice_int = new int[dice.Length];
List<int> loca = new List<int>();
int prise;
for (int index = 0; index < dice.Length; index++)
{
dice_int[index] = Int32.Parse(dice[index]);
}
for (int index = 0; index < dice_int.Length; index++)
{
if (index == 2)
{
if (dice_int[index] == dice_int[0])
{
loca.Add(index);
}
}
else if (dice_int[index] == dice_int[index + 1])
{
loca.Add(index);
}
}
if (loca.Count > 2)
{
prise = 10000 + dice_int[0] * 1000;
}
else if (loca.Count > 0)
{
prise = 1000 + dice_int[loca[0]] * 100;
}
else
{
int bignum = (dice_int[0] > dice_int[1]) ? dice_int[0] : dice_int[1];
bignum = (bignum > dice_int[2]) ? bignum : dice_int[2];
prise = bignum * 100;
}
Console.WriteLine(prise);
}
}
사실 2480 문제는 조건문을 엄청 때려박으면 더 쉽게 풀 수 있지만 약간의 가독성을 위해 리스트를 사용해봄
for문을 통해 배열 안에 있는 주사위 눈들을 비교 한 후 같다면 그 인덱스 값을 리스트에 Add
비교를 a b c가 있다면 (a, b) (b, c) (a, c) 세번 하기 때문에 리스트 loca.count를 통하여 리스트의 인덱스 갯수를 파악
3개 모두 같다면 count 값이 3이기때문에 (1보다 크면 모두 같은 눈) 조건은 1이나 2보다 크다고 잡아도 됨
2개가 같다면 count 값이 1, 같은 눈이 없다면 큰 주사위의 눈 기준으로 그냥 삼항식을 가독성을 위해 두번 사용
'BaekJoon[C#]' 카테고리의 다른 글
반복문 2439 (0) | 2022.09.19 |
---|---|
반복문 15552 (0) | 2022.09.18 |
반복문 10950 (0) | 2022.09.17 |
입출력과 사칙연산 2588 (0) | 2022.09.15 |
입출력과 사칙연산 10869 (0) | 2022.09.14 |