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();
}
}
댓글