Codingame『Conway Sequence』クリア

SequenceSequence / LordFerguson


 何かもっといいやり方がありそうな、なさそうな。



 どういう問題かと言うと、
初期値RからL番目のコンウェイシーケンスを求めろ、という問題。


例えば、初期値Rが2だとすると、1番目は
2
2番目は、1番目が「1個の2」なので
1 2
3番目は、2番目最初が「1個の1」次に「1個の2」なので
1 1 1 2
こんな感じで、続いていく。


それをL番目まで求めて表示しましょう…という問題。


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

/**
 * Auto-generated code below aims at helping you parse
 * the standard input according to the problem statement.
 **/
class Solution
{
    static void Main(string[] args)
    {
        //初期値
        int R = int.Parse(Console.ReadLine());
        //ライン数
        int L = int.Parse(Console.ReadLine());
        
        //コンウェイシーケンスを格納するリスト
        List<int> seq = new List<int>();
        //裏で使うやつ
        List<int> tmp = new List<int>();
        
        //1回目
        seq.Add(R);
        //門番として最後尾に0を加える
        seq.Add(0);
        
        for(int l = 0; l < L-1; l++) {
            //予備のクリア
            tmp.Clear();
            //カウンタの設置
            int count = 1;
            for(int p = 0; p < seq.Count-1; p++) {
                //次の数値が違ったら
                if(seq[p] != seq[p+1]) {
                    //カウンタと値の格納
                    tmp.Add(count);
                    tmp.Add(seq[p]);
                    //カウンタのリセット
                    count = 1;
                }
                else {
                    //次の数字が同じ数字ならカウンタを動かす
                    count++;
                }
            }

            //シーケンスクリア            
            seq.Clear();
            
            //予備から格納庫へコピー
            for(int i = 0; i < tmp.Count; i++) {
                seq.Add(tmp[i]);
                Console.Error.WriteLine(tmp[i]);
            }
            //門番の追加
            seq.Add(0);
            
        }
        // Write an action using Console.WriteLine()
        // To debug: Console.Error.WriteLine("Debug messages...");

        //出力
        for(int i = 0; i < seq.Count-1; i++) {
            Console.Write(seq[i]);
            //門番が出るまでスペースを挟む
            if(seq[i+1] != 0) Console.Write(" ");
        }
    }
}


Conway's All the World's Fighting Ships, 1922-1946 (Conways Naval History)

Conway's All the World's Fighting Ships, 1922-1946 (Conways Naval History)