分享

欧拉项目第四题 Problem4

 孤独求学者 2011-07-22

欧拉项目第四题 Problem4

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. 

单词:

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 + "不是回文数!");

        }

    }

}

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多