AtCoderのABC
次のABCの問題へ
前のABCの問題へ
ABC407-C Security 2
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("21");
//4
}
else if (InputPattern == "Input2") {
WillReturn.Add("407");
//17
}
else if (InputPattern == "Input3") {
WillReturn.Add("2025524202552420255242025524");
//150
}
else {
string wkStr;
while ((wkStr = Console.ReadLine()) != null) WillReturn.Add(wkStr);
}
return WillReturn;
}
static void Main()
{
List<string> InputList = GetInputList();
char[] CharArr = InputList[0].ToCharArray();
long UB = CharArr.GetUpperBound(0);
long PlusCnt = 0;
// 必要な階差が可能になったら、末尾に0を追加
for (long I = 0; I <= UB - 1; I++) {
long Val1 = CharArr[I] - '0';
long Val2 = CharArr[I + 1] - '0';
PlusCnt += (Val1 - Val2 + 10) % 10;
}
long CharCnt = CharArr.Length;
// 最後に+操作の分で調整
while (true) {
long Mod = PlusCnt % 10;
long FirstVal = CharArr[0] - '0';
if (Mod == FirstVal) break;
PlusCnt++;
}
Console.WriteLine(PlusCnt + CharCnt);
}
}
解説
階差は、+操作では変わらないので
必要な階差が可能になったら、末尾に0を追加してます。
最後に+操作の分で調整してます。