java資料產生法遍曆求從個位元都n位元字的迴文數演算法最佳化

來源:互聯網
上載者:User
public class PalindromeNumber2 {    /*產生一位元字的迴文數*/    private void adigit(long parentNumber, long tenID) {        long tenIDResult = (long) Math.pow(10, tenID);        for (int i = 0; i <= 9; i++) {            System.out.println((long) (parentNumber + (i * tenIDResult)));        }    }    /*產生兩位元字*/    private void twodigit(long parentNumber, long tenID, boolean iszero) {        int i = 0;        if (iszero) i = 0;        else i = 1;        long tenIDResult = (long) Math.pow(10, tenID);        for (; i <= 9; i++) {            System.out.println((long) (parentNumber + 11 * i * tenIDResult));        }    }    /*產生三位元字*/    private void threedigit(long parentNumber, long tenLID, boolean isThree, long tenID) {        long i = 1;        if (!isThree) i = 0;        for (; i <= 9; ) {            long result = parentNumber;            if (isThree) {                adigit((long) (i * Math.pow(10, 2) + i), tenID);            } else {                result += i * Math.pow(10, 2 * tenID - tenLID) + i * Math.pow(10, tenLID);                //    System.out.println("3位元"+i+"p...."+result+"i>>>>>"+i);                adigit(result, tenID);            }            ++i;        }    }    /*產生四位元字位元字*/    private void fourdigit(long parentNumber, long tenFID, long tenLID, boolean isfour, long tenID) {        long i = 1;        if (!isfour) i = 0;        for (; i <= 9; i++) {            if (isfour) {                twodigit((long) (i * Math.pow(10, 3) + i), tenID, true);            } else {                twodigit((long) (i * Math.pow(10, tenFID) + i * Math.pow(10, tenLID) + parentNumber), tenID - 1, true);            }        }    }    /*指定從個位元字產生到n位元字間的迴文數字列印*/    public void PalindromeNumber(long n) {        if (n <= 0) return;        if (n > 4) {            PalindromeNumber(4);            for (int i = 1; i <= n; i++) {                if (i == 1) {                    adigit(0, 0);                    continue;                } else if (i == 2) {                    twodigit(0, 0, false);                    continue;                } else if (i == 3) {                    threedigit(0, 0, true, 1);                    continue;                } else if (i == 4) {                    fourdigit(0, 0, 0, true, 1);                    continue;                }                oddDigitPalindromeNumber(i, 0, false, 0, 0);            }        }    }    public void palindromeNumber(long n) {        for (int i = 1; i < n; i++) {            PalindromeNumber(n);        }    }    public void oddDigitPalindromeNumber(long n, long tenId, boolean isZero, long PN, long lastTenId) {        int i = 0;        /*判斷開頭是否是0*/        if (!isZero) {            tenId = n / 2;            i = 1;        }        /*大於及等於5以上奇數位迴文數產生方法*/        if (n == 3) {            //  System.out.println("nT"+nextTenId);            threedigit(PN, lastTenId, false, tenId);            return;        }        /*大於及等於6以上偶數位迴文數產生方法*/        else if (n == 4) {            //  System.out.println("lId:::"+(2*tenId-lastTenId-1));            fourdigit(PN, 2 * tenId - lastTenId - 1, lastTenId, false, tenId);            return;        }        for (; i <= 9; ) {            long result = PN;            if (!isZero) {                PN = (long) (i * Math.pow(10, n - 1) + i);                result = PN;                lastTenId = 0;            } else {                result += (long) (i * Math.pow(10, 2 * tenId - lastTenId) + i * Math.pow(10, lastTenId));                //  System.out.println("res"+result);            }            //if (!isZero) System.out.println("i"+i);            ++i;            oddDigitPalindromeNumber(n - 2, tenId, true, result, lastTenId + 1);        }    }    /*大於及等於6以上奇數位迴文數*/    public void fire() {        oddDigitPalindromeNumber(6, 0, false, 0, 0);    }}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.