典型90問
次の典型90問へ
前の典型90問へ
典型90問 044 Shift and Swapping(★3)
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("8 5");
WillReturn.Add("6 17 2 4 17 19 1 7");
WillReturn.Add("2 0 0");
WillReturn.Add("1 7 2");
WillReturn.Add("1 2 6");
WillReturn.Add("1 4 5");
WillReturn.Add("3 4 0");
//4
}
else if (InputPattern == "Input2") {
WillReturn.Add("9 6");
WillReturn.Add("16 7 10 2 9 18 15 20 5");
WillReturn.Add("2 0 0");
WillReturn.Add("1 1 4");
WillReturn.Add("2 0 0");
WillReturn.Add("1 8 5");
WillReturn.Add("2 0 0");
WillReturn.Add("3 6 0");
//18
}
else if (InputPattern == "Input3") {
WillReturn.Add("11 18");
WillReturn.Add("23 92 85 34 21 63 12 9 81 44 96");
WillReturn.Add("3 10 0");
WillReturn.Add("3 5 0");
WillReturn.Add("1 3 4");
WillReturn.Add("2 0 0");
WillReturn.Add("1 4 11");
WillReturn.Add("3 11 0");
WillReturn.Add("1 3 5");
WillReturn.Add("2 0 0");
WillReturn.Add("2 0 0");
WillReturn.Add("3 9 0");
WillReturn.Add("2 0 0");
WillReturn.Add("3 6 0");
WillReturn.Add("3 10 0");
WillReturn.Add("1 6 11");
WillReturn.Add("2 0 0");
WillReturn.Add("3 10 0");
WillReturn.Add("3 4 0");
WillReturn.Add("3 5 0");
//44
//21
//34
//63
//85
//63
//21
//34
//96
}
else {
string wkStr;
while ((wkStr = Console.ReadLine()) != null) WillReturn.Add(wkStr);
}
return WillReturn;
}
static void Main()
{
List<string> InputList = GetInputList();
int[] AArr = InputList[1].Split(' ').Select(pX => int.Parse(pX)).ToArray();
int Len = AArr.Length;
int ShiftCnt = 0;
int[] wkArr = { };
Action<string> SplitAct = pStr =>
wkArr = pStr.Split(' ').Select(pX => int.Parse(pX)).ToArray();
Func<int, int> ChangeInd = (pInd) =>
{
int WillReturn = (pInd - ShiftCnt) % Len;
if (WillReturn < 0) WillReturn += Len;
return WillReturn;
};
foreach (string EachStr in InputList.Skip(2)) {
SplitAct(EachStr);
int T = wkArr[0];
if (T == 1) {
int Ind1 = wkArr[1] - 1;
int Ind2 = wkArr[2] - 1;
Ind1 = ChangeInd(Ind1);
Ind2 = ChangeInd(Ind2);
int tmp = AArr[Ind1];
AArr[Ind1] = AArr[Ind2];
AArr[Ind2] = tmp;
}
if (T == 2) {
ShiftCnt++;
}
if (T == 3) {
int Ind1 = wkArr[1] - 1;
Ind1 = ChangeInd(Ind1);
Console.WriteLine(AArr[Ind1]);
}
}
}
}
解説
回転シフトした回数を覚えておいて、
配列へのアクセス時に、modを取ってます。