zoukankan      html  css  js  c++  java
  • Project Euler Problem 32 Pandigital products

    Pandigital products

    Problem 32

    We shall say that an n-digit number is pandigital if it makes use of all the digits 1 ton exactly once; for example, the 5-digit number, 15234, is 1 through 5 pandigital.

    The product 7254 is unusual, as the identity, 39 × 186 = 7254, containing multiplicand, multiplier, and product is 1 through 9 pandigital.

    Find the sum of all products whose multiplicand/multiplier/product identity can be written as a 1 through 9 pandigital.

    HINT: Some products can be obtained in more than one way so be sure to only include it once in your sum.


    C++:

    #include <iostream>
    #include <algorithm>
    #include <set>
    
    using namespace std;
    
    int main()
    {
        int digits[] = {1, 2, 3, 4, 5, 6, 7, 8, 9};
        long left1, left2, mid1_4, mid2_4, right4;
        set<long> s;
    
        for(;;) {
            left1 = digits[0];
            left2 = digits[1] + digits[0] * 10;
            mid1_4 = digits[1] * 1000 + digits[2] * 100 + digits[3] * 10 + digits[4];
            mid2_4 = digits[2] * 100 + digits[3] * 10 + digits[4];
            right4 = digits[5] * 1000 + digits[6] * 100 + digits[7] * 10 + digits[8];
    
            if((left1 * mid1_4 == right4) || (left2 * mid2_4 == right4))
                s.insert(right4);
    
            if(!next_permutation(digits, digits+9))
                break;
        }
    
        long total = 0;
        for(set<long>::iterator iter=s.begin(); iter!=s.end(); iter++)
            total += *iter;
    
        cout << total << endl;
    
        return 0;
    }


    C++:

    #include <iostream>
    #include <cstring>
    #include <set>
    
    using namespace std;
    
    const long N1 = 100;
    const long N2 = 2000;
    
    bool ispandigital(long a, long b, long product)
    {
        int digits[10], d;
    
        memset(digits, 0, sizeof(digits));
    
        while(a) {
            d = a % 10;
            if(digits[d])
                return false;
            digits[d] = 1;
            a /= 10;
        }
        if(digits[0])
            return false;
    
        while(b) {
            d = b % 10;
            if(digits[d])
                return false;
            digits[d] = 1;
            b /= 10;
        }
        if(digits[0])
            return false;
    
        while(product) {
            d = product % 10;
            if(digits[d])
                return false;
            digits[d] = 1;
            product /= 10;
        }
        if(digits[0])
            return false;
    
        for(int i=1; i<10; i++)
            if(digits[i] == 0)
                return false;
    
        return true;
    }
    
    int main()
    {
        long long total = 0;
        long product;
        set<long> s;
    
        for(long i=1; i<=N1; i++)
            for(long j=i+1; j<=N2; j++) {
                product = i * j;
                if(product > 1000 && ispandigital(i, j, product))
                    s.insert(product);
            }
    
        total = 0;
        for(set<long>::iterator iter=s.begin(); iter!=s.end(); iter++)
            total += *iter;
    
        cout << total << endl;
    
        return 0;
    }


  • 相关阅读:
    九连环
    杨辉三角
    魔术师发牌问题(循环链表)
    Linux 技巧:让进程在后台可靠运行的几种方法
    博客新地址
    x&(-x)取x的最后一个1的证明
    c++对象模型布局分析
    c++ 子类要正确的调用父类构造函数
    hibernate ID 生成方式
    IOCP
  • 原文地址:https://www.cnblogs.com/tigerisland/p/7563994.html
Copyright © 2011-2022 走看看