[백준] C# : 전자레인지 (10162번)

ssu_hyun·2022년 8월 4일
0

Data Structure & Algorithm

목록 보기
47/67
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Baekjoon
{
    class Program
    {
        // 그리디 알고리즘
        static void Main(string[] args)
        {
            int t = int.Parse(Console.ReadLine());
            int[] sec = { 300, 60, 10 };
            int[] button = { 0, 0, 0 };

            for (int i=0; i < sec.Length; i++)
            {
                if (t/sec[i] > 0)  // 나눌 수 있을 때(몫이 0보다 클 때)
                {
                    button[i] = t / sec[i];  // 몫
                    t = t % sec[i];  // 나머지
                }
                else
                {
                    button[i] = 0;
                }
            }
            if (t != 0)
            {
                Console.WriteLine(-1);
            }
            else
            {
                Console.WriteLine($"{button[0]} {button[1]} {button[2]}");
            }
        }
    }
}
/*
300, 60, 10은 모두 10의 배수이고 10보다 큰 수들이므로 t를 10으로 나누었을 때 나머지가 존재한다면
1의자리인 초단위가 있다는 뜻이므로 A, B, C 만으로는 시간을 정확하게 맞출 수가 없다.
따라서 이를 제외한 경우만 계산을 진행한다.
*/

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Baekjoon
{
    class Program
    {
        // 그리디 알고리즘
        static void Main(string[] args)
        {
            int t = int.Parse(Console.ReadLine());
            int[] sec = { 300, 60, 10 };
            int[] button = { 0, 0, 0 };

            // 최소단위인 10초로 나누어 떨어지는지 확인
            if (t % 10 != 0)
            {
                Console.WriteLine(-1);
            }
            else
            {
                for (int i = 0; i < sec.Length; i++)
                {
                    if (t / sec[i] > 0)  // 나눌 수 있을 때(몫이 0보다 클 때)
                    {
                        button[i] = t / sec[i];  // 몫
                        t = t % sec[i];  // 나머지
                    }
                    else
                    {
                        button[i] = 0;
                    }
                }
                Console.WriteLine($"{button[0]} {button[1]} {button[2]}");
            }
        }
    }
}

0개의 댓글