본문 바로가기

프로그래밍/알고리즘

C#에서 말하기 수열(개미 수열) 구하기

using System;

namespace Practice_Page
{
    class Program
    {
        static void Main(string[] args)
        {
            while (true)
            {
                int[] array = new int[100];
                for (int i = 0; i < array.Length; i++)
                {
                    array[i] = Console.Read();
                    if (array[i] == 13)
                        break;
                }

                int count = 0;
                int index = 0;
                Console.Write(">> ");

                for (int i = 0; i < array.Length; i++)
                {
                    if (array[i] == array[index])
                    {
                        count++;
                    }
                    else
                    {
                        Console.Write((char)array[index] + "" + count);
                        if (array[i] == 13)
                        {
                            array[0] = Console.Read();    // [34] ~ [35] 버퍼값 삭제하는 법을 몰라서 대충 때웠다.
                            array[0] = 0;
                            Console.WriteLine();
                            break;
                        }
                        else
                        {
                            index = i;
                            i -= 1;
                            count = 0;
                        }
                    }
                }
            }
        }
    }
}

 

입력 받은 수열의 개미 수열을 구하는 코드.

입력 받은 개행문자를 지우는 법을 몰라 땜빵한 부분이 부끄럽다.