본문 바로가기
개발공간

C# Number Line Jumps [HackerRank]

by -0o0- 2021. 9. 9.

C# Number Line Jumps [HackerRank]

using System.CodeDom.Compiler;
using System.Collections.Generic;
using System.Collections;
using System.ComponentModel;
using System.Diagnostics.CodeAnalysis;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Runtime.Serialization;
using System.Text.RegularExpressions;
using System.Text;
using System;

class Result
{
    public static string kangaroo(int x1, int v1, int x2, int v2)
    {
        //x1 is first kangaroo point, v1 is first kangaroo jump length
        //x2 is first kangaroo point, v2 is first kangaroo jump length
        
        int firstKangaroo = x1;
        int secondKangaroo = x2;
        
        //두번째 캥거리 이동길이가 더 크면 같아질수 없으므로 NO 반환
        if(v2>v1) return "NO";
        
        //두번째 캥거루가 큰 동안 반복
        while(firstKangaroo<secondKangaroo)
        {
            //각 캥거루에 이동 길이 더하기
            firstKangaroo += v1;
            secondKangaroo += v2;
            
            //첫번째 캥거루와 두번째 캥거루가 같으면 YES 반환
            if(firstKangaroo==secondKangaroo) return "YES";
        }
        
        //반복을 끝내고도 반환하지 못한경우 NO 반환
        return "NO";
    }

}

class Solution
{
    public static void Main(string[] args)
    {
        TextWriter textWriter = new StreamWriter(@System.Environment.GetEnvironmentVariable("OUTPUT_PATH"), true);

        string[] firstMultipleInput = Console.ReadLine().TrimEnd().Split(' ');

        int x1 = Convert.ToInt32(firstMultipleInput[0]);

        int v1 = Convert.ToInt32(firstMultipleInput[1]);

        int x2 = Convert.ToInt32(firstMultipleInput[2]);

        int v2 = Convert.ToInt32(firstMultipleInput[3]);

        string result = Result.kangaroo(x1, v1, x2, v2);

        textWriter.WriteLine(result);

        textWriter.Flush();
        textWriter.Close();
    }
}

'개발공간' 카테고리의 다른 글

C# Subarray Division [HackerRank]  (0) 2021.09.09
C# Breaking the Records [HackerRank]  (0) 2021.09.09
C# Apple and Orange [HackerRank]  (0) 2021.09.09
C# Time Conversion [HackerRank]  (0) 2021.08.31
C# Birthday Cake Candles [HackerRank]  (0) 2021.08.31

댓글