AtCoderのABC
次のABCの問題へ
前のABCの問題へ
ABC247-C 1 2 1 3 1 2 1
C#のソース
using System;
using System.Collections.Generic;
using System.Linq;
class Program
{
static string InputPattern = "InputX";
static List<string> GetInputList()
{
var WillReturn = new List<string>();
if (InputPattern == "Input1") {
WillReturn.Add("2");
//1 2 1
}
else if (InputPattern == "Input2") {
WillReturn.Add("1");
//1
}
else if (InputPattern == "Input3") {
WillReturn.Add("4");
//1 2 1 3 1 2 1 4 1 2 1 3 1 2 1
}
else {
string wkStr;
while ((wkStr = Console.ReadLine()) != null) WillReturn.Add(wkStr);
}
return WillReturn;
}
static void Main()
{
List<string> InputList = GetInputList();
int N = int.Parse(InputList[0]);
var PrevList = new List<int>();
PrevList.Add(1);
for (int I = 2; I <= N; I++) {
var CurrList = new List<int>();
CurrList.AddRange(PrevList);
CurrList.Add(I);
CurrList.AddRange(PrevList);
PrevList = CurrList;
}
Console.WriteLine(IntEnumJoin(" ", PrevList));
}
////////////////////////////////////////////////////////////////
// セパレータとInt型の列挙を引数として、結合したstringを返す
////////////////////////////////////////////////////////////////
static string IntEnumJoin(string pSeparater, IEnumerable<int> pEnum)
{
string[] StrArr = Array.ConvertAll(pEnum.ToArray(), pX => pX.ToString());
return string.Join(pSeparater, StrArr);
}
}
解説
ListのAddRangeメソッドを使ってます。