BaekJoon

백준 알고리즘 1330번 : 두 수 비교하기

코샵 2023. 9. 11. 15:53
반응형

 

문제에 A,B가 주어진다고 써있지만 예제 입력이라는 부분이 있는걸 보고 이와같은 코드를 작성했다.

using System;
namespace BaekJoon
{
    public class _330
    {
        public static void Main()
        {
            Console.WriteLine();

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

            int.TryParse(number[0], out int a);
            int.TryParse(number[1], out int b);

            if(IsInRange(a) && IsInRange(b))
            {
                if (a > b) Console.Write(">");
                else if (a < b) Console.Write("<");
                else if (a == b) Console.Write("==");
            }
        }

        public static bool IsInRange(int target) => (target >= -10000 && target <= 10000);
    }
}

테스트를 거친 후 정답 제출을 하였지만 "출력 형식이 잘못되었습니다" 라는 결과가 나왔다... 

무엇이 문제일까 생각하며 다시 문제를 보았는데 A와 B가 주어진다라는 것을 보고 설마했다. 처음에 문제를 접했을 때도 주어진다길래 Random을 이용해 -10000~10000 사이의 임의의 정수를 뽑아내 사용하려고 했기에 그래도 혹시 몰라 난수 생성을 해 문제 제출을 했지만 이번엔 "틀렸습니다"라는 결과가 나왔다... 

using System;
namespace BaekJoon
{
    public class _330
    {
        public static void Main()
        {
            var a = generateRandomInt(-10000, 10000);
            var b = generateRandomInt(-10000,10000);
            Console.Write($"{a} {b}");

            if (a > b) Console.Write(">");
            else if (a < b) Console.Write("<");
            else if (a == b) Console.Write("==");
          
        }

        public static int generateRandomInt(int minValue, int maxValue)
        {
            Random random = new Random();
            return random.Next(minValue, maxValue);
        }
     }
}

입력도 첫줄 출력도 첫줄이라길래 WriteLine을 안해야하나 싶었는데 이것도 아니였나보다

 

내가 독해력이 떨어지는지 아무리 살펴봐도 모르겠다

2023.09.11 


다른 문제를 풀다가 같은 문제점이 나와 해결 후 재시도 했더니 성공했다.

 

using System;
namespace backjoon
{
    public class _330
    {
        public static void Main()
        {
            string[] number = Console.ReadLine().Split(' ');

            int.TryParse(number[0], out int a);
            int.TryParse(number[1], out int b);

            if(IsInRange(a) && IsInRange(b))
            {
                if (a > b) Console.WriteLine(">");
                else if (a < b) Console.WriteLine("<");
                else if (a == b) Console.WriteLine("==");
            }
        }

        public static bool IsInRange(int target) => (target >= -10000 && target <= 10000);
    }
}

Console.WriteLine();  << 이게 문제였다