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;
    }


  • 相关阅读:
    MYSQL 优化(二),持续更新收藏
    一些linux命令 备份下
    lsyncd +xinetd+syncd 多服务器文件同步
    阿里slb+ecs+https
    微擎 从 php5 到php7 的各种填坑 持续更新
    lmap
    微擎的ifp ife ifpp
    工具索引 mark名字
    Funny Bug || Sky Hole
    mysql 查询小技巧
  • 原文地址:https://www.cnblogs.com/tigerisland/p/7563994.html
Copyright © 2011-2022 走看看