どーも、みつおです。
問題
585 = 1001001001 (2進) は10進でも2進でも回文数である.
100万未満で10進でも2進でも回文数になるような数の総和を求めよ.
(注: 先頭に0を含めて回文にすることは許されない.)
出典:Problem36
解答
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 |
using System; using System.Collections.Generic; using System.Linq; namespace Problem36 { class Program { static void Main(string[] args) { Console.WriteLine(Solve()); Console.ReadLine(); } private static long Solve() { long ret = 0; List<long> tmp = new List<long>(); for (int i = 1; i < 1000000; i++) { //10進数が回文かチェック if (IsKaibun(i)) { //2進数が回文かチェック if (IsKaibunNisinsu(i)) { tmp.Add(i); } } } //総和 ret = tmp.Sum(); return ret; } private static bool IsKaibun(int no) { string s = no.ToString(); //回文か判定 return s.Reverse().SequenceEqual(s); } private static bool IsKaibunNisinsu(int no) { //2進数に変換 string s = Convert.ToString(no, 2); //回文か判定 return s.Reverse().SequenceEqual(s); } } } |
出力
872187
コメントを残す