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

前端 未结 21 1497
感情败类
感情败类 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:23

    As a very simple solution, this one works

    public class LargestPallendrome {
    
    public static void main(String[] args) {
    
        int a = 999;
        int b = 999;
    
        long max = 0;
    
        while (a > 100) {
            long num = a * b;
            if (checkPallendrome(num)) {
                if (num > max)
                    max = num;
            }
            if (b >= 100)
                b--;
            else {
                a--;
                b = 999;
            }
        }
        System.out.println(max);
    }
    
    public static boolean checkPallendrome(long num) {
        String a = num + "";
        String b = new StringBuffer(num + "").reverse().toString();
        if (a.equals(b))
            return true;
        return false;
    }
    }
    

提交回复
热议问题