1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

p45 Best Cow Line(C#)

Last updated at Posted at 2022-03-16

自分(正解)

using System;
using System.Diagnostics.CodeAnalysis;
using System.Reflection;
using System.Runtime.ExceptionServices;

public class Program
{
    public static void Main()
    {
        int N = 6;
        string S = "ACDBCB";
        string T = "";
        char[] list_S = { };

        //(1)Sをstringから配列に変える
        list_S = S.ToCharArray();

        //(2)Sが空になるまでループ
        while (list_S.Any()) 
        {
            //(3)最初と最後を取り出す
            char first = list_S[0];
            char second = list_S.Last();
            Console.WriteLine("firstは{0},secondは{1}", first, second);

            //(4)比較して、小さい方を別の変数に取り出す
            if (first < second)
            { //firstの方が小さい
              //(5)先頭削除、Tにはpushする
                Console.WriteLine("first");
                T += first.ToString();
                list_S = removeChar(0, list_S); 
            }
            else if (first >= second)
            { //secondの方が小さい
                Console.WriteLine("second");
              //(5)末尾削除、Tにはpushする
                T += second.ToString();
                list_S = removeChar(list_S.Length - 1, list_S);
            }
        }
            Console.WriteLine("答え:{0}",T);

        char[] removeChar(int indexToRemove, char[] list_S)
        {
            return list_S.Where((source, index) => index != indexToRemove).ToArray(); 
        }


    }
}

終わってみて

配列じゃなくて、List使ったらもっと追加、削除のところが簡単だったかも

var list = new List<string>() {"Alice", "Bob", "Charlie", "Dave"};
list.Add("Eve");
1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?