Find the largest palindrome made from the product of two 3-digit numbers - Javascript

前端 未结 21 1439
感情败类
感情败类 2020-12-28 17:16

Can anyone tell me what\'s wrong with the code. Find the largest palindrome made from the product of two 3-digit numbers.

function largestPalind         


        
21条回答
  •  时光取名叫无心
    2020-12-28 17:50

    public static void main(String[] args) {
        int tempAns = 0;
        int max = 999;
        for (int i = 100; i <= max; i++) {
            for (int j = max; j >= i; j--) {
                if (findPalindrome(i * j) && (i * j) > tempAns) {
                    System.out.println("Palindrome: " + j + " * " + i + " = " + j * i);
                    tempAns = i * j;
                }
            }
        }
    }
    
    private static boolean findPalindrome(int n) {
        String nString = String.valueOf(n);
        int j = 0;
        int stringLength = nString.length() - 1;
        for (int i = stringLength; i >= 0; i--) {
    
            if (nString.charAt(j) == nString.charAt(i)) {
    
                if (i == 0) {
                    return true;
                }
                j++;
    
            } else if (nString.charAt(j) != nString.charAt(i)) {
                return false;
            }
    
        }
    
        return false;
    }
    

提交回复
热议问题