C# 1193 분수찾기

https://www.acmicpc.net/problem/1193

이 문제는 내가 충분한 감각과 비 수학적 지식이 있다면 어떻게 풀 수 있을지 단번에 생각하게 만드는 문제였던 것 같다.

처음에는 답을 찾기 위해 2차원 배열을 만들고 초기화하는 방법을 생각했지만

문제 자체가 시간이 얼마 남지 않아서 그렇게 초기화할 시간이 없을 것 같아서 깔끔하게 2차원 배열을 버렸습니다.

그냥 생각해

오고 가는 것

N/1 또는 1/N에서 N은 짝수 변은 위에서 아래로, 홀수 변은 아래에서 위로 이어지는 대각선이며,

(입력값 X – 1부터 i번째까지의 합(X보다 작은 최대값) – 1)번째 값이 정답입니다.

말로 설명하려면 시간이 오래 걸리므로 코드를 실행해 보면 이해하기 쉽습니다.

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

namespace BaekJoon_s_Note
{
    class Fountain
    {
        int start_X;
        int X;
        int Y;
        int Go;

        public Fountain(int X)
        {
            this.start_X = X;

            MovingMan();
            Console.WriteLine($"{this.Y}/{this.X}");
        }

        public bool Start()
        {
            int sum = 0;
            int i = 1;

            while (sum + i < start_X)
            {
                sum += i;
                i++;
            }

            Go = this.start_X - sum - 1;

            if (i % 2 == 0)
            {
                X = i;
                Y = 1;
                return true;
            }
            else
            {
                Y = i;
                X = 1;
                return false;
            }
        }
        public void MovingMan()
        {
            bool check = Start();
            for (int i = 0; i < Go; i++)
            {
                if (check)
                {
                    X -= 1;
                    Y += 1;
                }
                else
                {
                    X += 1;
                    Y -= 1;
                }
            }
        }
    }

    class Program
    {
        public static void Main(string() args)
        {
            Fountain fountain = new Fountain(int.Parse(Console.ReadLine()));
        }
    }
}


그것은 사실이지만 뭔가 빠졌습니다.

다른 사람의 코드를 살펴보고 뭔가를 배울 수 있는지 알아보고 두 개의 루프를 사용했습니다.

높은 수준의 사람들은 루프를 한 번만 작성했습니다.

무빙맨 기능에서 일일이 값을 추가할 필요가 없습니다.

이것은 1+5 = 6일 때 1+1+1+1+1+1 = 6을 계산하는 것과 같기 때문에 사용할 필요가 없는 반복문입니다.

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

namespace BaekJoon_s_Note
{
    class Fountain
    {
        int start_X;
        int X;
        int Y;
        int Go;

        public Fountain(int X)
        {
            this.start_X = X;

            MovingMan();
            Console.WriteLine($"{this.Y}/{this.X}");
        }

        public bool Start()
        {
            int sum = 0;
            int i = 1;

            while (sum + i < start_X)
            {
                sum += i;
                i++;
            }

            Go = this.start_X - sum - 1;

            if (i % 2 == 0)
            {
                X = i;
                Y = 1;
                return true;
            }
            else
            {
                Y = i;
                X = 1;
                return false;
            }
        }
        public void MovingMan()
        {
            bool check = Start();
            if (check)
            {
                X -= Go;
                Y += Go;
            }
            else
            {
                X += Go;
                Y -= Go;
            }
        }
    }

    class Program
    {
        public static void Main(string() args)
        {
            Fountain fountain = new Fountain(int.Parse(Console.ReadLine()));
        }
    }
}


기억력은 어떻게 증가했는가? 나에게 시간을 주지 않았어?

암튼 정신만 차리면 10번은 알아낼 수 있기 때문에 실버5에 해당하는 문제였다고 생각합니다.