欧拉项目第四题 Problem4 A palindromic number reads the same both ways. The largest palindrome made from the product of two 2-digit numbers is 9009 = 91 Find the largest palindrome made from the product of two 3-digit numbers. 单词: palindromic:回文 A palindromic number:回文数 Product:产品、产物、乘积 the product of two 2-digit numbers:两个2位数的乘积 public class Problem4 { /**欧拉项目第4题 * 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. * 求两个三位数相乘能都得到的最大回文数 * @param args */ /**判断是否是回文数 * 首先:int==>String * 然后:将字符串翻转后与原字符串相比较,相等则为回文数 */ public boolean isPalindromic(int intNum){ String strNum = Integer.toString(intNum); //int==>String StringBuffer sb = new StringBuffer(); sb.append(strNum); //String==>StringBuffer StringBuffer sbRollBack = new StringBuffer(); sbRollBack.append(sb.reverse()); //翻转字符串 String strNumTwo = sbRollBack.toString(); //StringBuffer==>String if(strNum.equals(strNumTwo)){ return true; }else{ return false; } } public boolean isPalindromic2(int intNum){ String strNum = Integer.toString(intNum); //int==>String StringBuffer sb = new StringBuffer(); sb.append(intNum); //String==>StringBuffer StringBuffer sbRollBack = new StringBuffer(); sbRollBack.append(sb.reverse()); //翻转字符串 String strNumTwo = sbRollBack.toString(); //StringBuffer==>String if(strNum.equals(strNumTwo)){ return true; }else{ return false; } } public static void main(String[] args) { Problem4 p4 = new Problem4(); int a, b, c; int max = 0; for(a = 999; a > 99; a--){ for(b = 999; b >= a; b--){ c = a * b; //如果c小于新生产的回文数,则退出 if(c < max){ break; } if(p4.isPalindromic(c)){ //可用表达式if(p4.isPalindromic2(c)) max = c; } //System.out.println("回文数为:" + max); } } System.out.println("回文数为:" + max); //p4.textIsPalindromic(); } /** * 用于测试函数isPalindromic()的功能 * */ public void textIsPalindromic(){ int i = 1234321; if(isPalindromic(i)){ System.out.println(i + "是回文数!"); }else{ System.out.println(i + "不是回文数!"); } } } |
|