Problem 4
16 November 2001
A palindromic number reads the same both ways. The largest palindrome made from the product of two 2-digit numbers is 9009 = 91 × 99.
Find the largest palindrome made from the product of two 3-digit numbers.
ある回文数は前から読んでも後ろから読んでも同じ数です。(例:13531とか169961)wiki
2桁の数字の積で一番大きくなる回文数は9009で91*99。
では3桁の数字の積で一番大きくなる回文数を求めましょう。(日本語訳PNN)
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
/*
Problem 4
16 November 2001
A palindromic number reads the same both ways. The largest palindrome made from the product of two 2-digit numbers is 9009 = 91 × 99.
Find the largest palindrome made from the product of two 3-digit numbers.
*/
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
double a = 999;
double b = 0;
double max = 0;
string maxstr = "";
for (double i = 0; i <= a; i++)
{
for (double j = 0; j <= i; j++)
{
b = (a - i) * (a - j);
//回文数か判断する処理
bool kaibun = true;
double c = b;
int[] keta = new int[b.ToString().Length+1];
for (int k = 1; k <= b.ToString().Length; k++)
{
keta[k] = int.Parse(b.ToString().Substring(k-1, 1));
}
if ((int)(b.ToString().Length / 2) * 2 == b.ToString().Length)
{
for (int l = 1; l <= b.ToString().Length / 2; l++)
{
if (keta[l] != keta[b.ToString().Length - l + 1])
{
kaibun = false;
break;
}
}
}
else
{
for (int l = 1; l <= (int)(b.ToString().Length / 2); l++)
{
if (keta[l] != keta[b.ToString().Length - l + 1])
{
kaibun = false;
break;
}
}
}
if (kaibun == true)
{
if (b >= max)
{
max = b;
maxstr = b.ToString() + " = " + (a - i).ToString() + " * " + (a - j).ToString();
}
}
}
}
Console.WriteLine(maxstr);
Console.ReadLine();
}
}
}
出力結果:906609 = 913 * 993
0 件のコメント:
コメントを投稿