• Project Euler Problem 4: Largest palindrome product


    Largest palindrome product

    Problem 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.


    C++:

    #include <iostream>
    
    using namespace std;
    
    const int FROM = 100;
    const int TO = 999;
    
    bool ispalindrome(int product)
    {
        int miror = 0, temp;
    
        temp = product;
        while(temp) {
            miror *= 10;
            miror += temp % 10;
    
            temp /= 10;
        }
    
        return miror == product;
    }
    
    int main()
    {
        int maxpalindrome = 0, temp;
    
        for(int i=TO; i>=FROM; i--)
            for(int j=TO; j>=FROM; j--) {
                temp = i * j;
                if(temp < maxpalindrome)
                    break;
    
                if(ispalindrome(temp))
                    if(temp > maxpalindrome)
                        maxpalindrome = temp;
            }
    
        cout << maxpalindrome << endl;
    
        return 0;
    }



    C++:

    #include <iostream>
    #include <queue>
    #include <cstdio>
    #include <cstring>
    
    using namespace std;
    
    const int MAXN = 1000000;
    const int FROM = 100;
    const int TO = 999;
    
    struct node {
        int a, b, product;
        bool operator < (const node& n) const {
            return product < n.product;
        }
    };
    
    bool ispalindrome1(int product)
    {
        char s[MAXN+1];
        int start, end;
        bool isp = true;
    
        sprintf(s, "%d", product);
    
        start = 0;
        end = strlen(s)-1;
        while(start <= end) {
            if(s[start] != s[end]) {
                isp = false;
                break;
            }
            start++;
            end--;
        }
    
        return isp;
    }
    
    bool ispalindrome2(int product)
    {
        int miror = 0, temp;
    
        temp = product;
        while(temp) {
            miror *= 10;
            miror += temp % 10;
    
            temp /= 10;
        }
    
        return miror == product;
    }
    
    int main()
    {
        priority_queue<node> q;
        node t;
    
        for(int i=FROM; i<=TO; i++)
            for(int j=FROM; j<=TO; j++) {
                t.a = i;
                t.b = j;
                t.product = i * j;
    
                q.push(t);
            }
    
        while(!q.empty()) {
            t = q.top();
            q.pop();
    
            if(ispalindrome2(t.product)) {
                cout << t.product << endl;
                break;
            }
        }
    
        return 0;
    }


    Run results:

    906609


  • 相关阅读:
    【刷题】洛谷 P2764 最小路径覆盖问题
    【刷题】BZOJ 3546 [ONTAK2010]Life of the Party
    【刷题】BZOJ 3175 [Tjoi2013]攻击装置
    【刷题】BZOJ 4516 [Sdoi2016]生成魔咒
    【刷题】SPOJ 1811 LCS
    【刷题】洛谷 P3804 【模板】后缀自动机
    【刷题】SPOJ 8222 NSUBSTR
    (98)Wangdao.com_第三十天_拖拉事件
    ECMA Script 6_必须要知道的基础
    (97)Wangdao.com_第三十天_触摸事件
  • 原文地址:https://www.cnblogs.com/tigerisland/p/7564042.html
Copyright © 2020-2023  润新知