AtCoderのPAST
次のPASTの問題へ
前のPASTの問題へ
第7回PAST G べき表現
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("6");
//2
//9 -3
}
else if (InputPattern == "Input2") {
WillReturn.Add("9193");
//9
//2187 27 1 -243 3 9 -81 6561 729
}
else if (InputPattern == "Input3") {
WillReturn.Add("10120190919012");
//16
//-1594323 9 -177147 -531441 1162261467 -4782969 387420489 -6561 -2187 2541865828329 -27 7625597484987 3486784401 10460353203 -94143178827 31381059609
}
else {
string wkStr;
while ((wkStr = Console.ReadLine()) != null) WillReturn.Add(wkStr);
}
return WillReturn;
}
static void Main()
{
List<string> InputList = GetInputList();
long N = long.Parse(InputList[0]);
// 平衡3進数に変換
var NumList = new List<long>();
do {
long Mod = N % 3;
if (Mod == 0) {
N /= 3;
NumList.Add(0);
}
if (Mod == 1) {
N /= 3;
NumList.Add(1);
}
if (Mod == 2) {
N++;
N /= 3;
NumList.Add(-1);
}
} while (N > 0);
long BekiVal = 1;
var AnswerList = new List<long>();
foreach (long EachNum in NumList) {
AnswerList.Add(EachNum * BekiVal);
BekiVal *= 3;
}
// 0はRemoveする
AnswerList.RemoveAll(pX => pX == 0);
Console.WriteLine(AnswerList.Count);
string[] AnswerStr = Array.ConvertAll(AnswerList.ToArray(), pX => pX.ToString());
Console.WriteLine(string.Join(" ", AnswerStr));
}
}
解説
平衡3進数に変換してます。