# 找到由两个3位数的乘积组成的最大回文。（Java）[英] Find the largest palindrome made from the product of two 3-digit numbers. (Java)

### 问题描述

public class Main {

public static void main(String[] args) {
int num = 998001;
int count=0;
int temp1, temp2, temp3, temp4, temp5, temp6;
int temp;
int num1=999;
int num2=100;
for (int i = 100000; i <= 998001; i++) {
num=998001-count;
temp=num;
temp1=temp%10;
temp=temp/10;
temp2=temp%10;
temp=temp/10;
temp3=temp%10;
temp=temp/10;
temp4=temp%10;
temp=temp/10;
temp5=temp%10;
temp=temp/10;
temp6=temp%10;
temp=temp/10;

if (temp1==temp6 && temp5==temp2 && temp3==temp4) {
System.out.println(num);
}
count=count+1;
}
}
}

## 推荐答案

//save the largest number
int largest = 0;

//loop over every possible product of numbers from 100-999
for (int i = 999; i >= 100; i--) {
for (int j = i; j >= 100; j--) {
int curr = i * j;

//if the current number is a palindrome and is greater than the last found largest
if (isPalindrome(curr) && curr > largest) {

//save it as the new largest found number
largest = curr;
}
}
}

isPalindrome 方法可能如下所示:

private static boolean isPalindrome(Integer possible) {
String toStr = possible.toString();
int len = toStr.length();
if (len % 2 == 1) {
len = len - 1;
}

for (int i = 0; i < len / 2; i++) {
if (toStr.charAt(i) != toStr.charAt(toStr.length() - (1 + i))) {
return false;
}
}

return true;
}