Game Development, 게임개발/개발

가장 큰 값부터 없애기 - C#

게임이 더 좋아 2021. 5. 28. 16:12
반응형
728x170

 

뭐 어렵지 않은데 

어떤 언어로도 구현할 수 있으려면 내 생각 과정을 자세히 적어야 할 것 같아서 기록한다.

 

https://www.codingame.com/ide/puzzle/the-descent

 

 


 

#풀이

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

/**
 * The while loop represents the game.
 * Each iteration represents a turn of the game
 * where you are given inputs (the heights of the mountains)
 * and where you have to print an output (the index of the mountain to fire on)
 * The inputs you are given are automatically updated according to your last actions.
 **/
class Player
{
    static void Main(string[] args)
    {
        int idx = 0;
        // game loop
        while (true)
        {
            int height = 0;
            for (int i = 0; i < 8; i++)
            {
                int mountainH = int.Parse(Console.ReadLine());
                 // represents the height of one mountain.
                if(mountainH > height){
                    idx = i;
                    height = mountainH;
                }
            }
            // Write an action using Console.WriteLine()
            // To debug: Console.Error.WriteLine("Debug messages...");

            Console.WriteLine(idx); // The index of the mountain to fire on.

        }
    }
}

 

 

우선 생각이 그렇다.

 

한 번 while문이 돌 때

가장 큰 값의 index를 구해야한다.

for문으로 입력을 모두 읽었을 때 최댓값에 해당하는 index를 가지고 있어야 한다.

 

최댓값이므로 constraints에서 최솟값이거나 최솟값보다 조금 작은 값을 기준을 잡는다. 

(나의 경우에는 Height = 0)

for 문 index로 조사하면서 (입력이 순서대로 되고, index가 0부터 시작함)

최대값을 갱신해주고 마지막으로 최대값을 입력한 i만이 index에 남게 된다.

 

그 idx를 출력해주면 된다.

 

** 여기서는 자동으로 그 최댓값의 index에 있는 숫자가 0으로 초기화된다.즉, 여기서 idx로 출력된 값을 이용해서 해당 라인에 있는 숫자를 0으로 할당하는 구문이 있어야 하지만 편의상 지운듯 하다.

 

 

반응형
그리드형