一個迴文數指的是從左向右和從右向左讀都一樣的數字。最大的由兩個兩位數乘積構成的迴文數是9009 = 91 * 99.
找出最大的有由個三位數乘積構成的迴文數。
原題目鏈接:Problem 4
還是不能暴力算,因爲是求最大的迴文數,所以從最大的開始減
public class Problem4 { public static void main(String[] args) { int res = countPalindromic(); System.out.println(res); } public static int countPalindromic() { int min = 100001; int max = 999999; int res = 0; int value = 0; for (int a = 999; a > 100; a--) { for (int b = max / a; b > 100; b--) { if(b > 999) continue; value = a * b; if (value > max || value < min) break; if (isPalindromic(value) && value > res) { res = value; System.out.println("value:" + value + ",a:" + a + ",b:" + b); } } } return res; } public static Boolean isPalindromic(int value) { String a = String.valueOf(value); String b = new StringBuffer(a).reverse().toString(); return a.equals(b); } }