Project Euler:Problem 38 Pandigital multiples

Take the number 192 and multiply it by each of 1, 2, and 3:

192 × 1 = 192

192 × 2 = 384

192 × 3 = 576

By concatenating each product we get the 1 to 9 pandigital, 192384576. We will call 192384576 the concatenated product of 192 and (1,2,3)

The same can be achieved by starting with 9 and multiplying by 1, 2, 3, 4, and 5, giving the pandigital, 918273645, which is the concatenated product of 9 and (1,2,3,4,5).

What is the largest 1 to 9 pandigital 9-digit number that can be formed as the concatenated product of an integer with (1,2, ... , n)
where n > 1?

#include <iostream>
#include <string>
#include <map>
using namespace std;

bool pan_mul(int a, int b,int &c)
{
	c = 0;
	map<int, int>mp;
	for (int i = 1; i <= b; i++)
	{
		int tmp = a*i;
		int num = tmp;
		int count = 0;
		while (tmp)
		{
			if (mp[tmp % 10] != 0)
				return false;
			mp[tmp % 10]++;
			tmp /= 10;
			count++;
		}
		if (mp[0] != 0)
			return false;
		if (i == 1)
			c += num;
		else
			c = c*pow(10, count) + num;
	}
	if (mp[0] != 0)
		return false;
	if (mp.size() == 10)
		return true;
	else
		return false;
}

int main()
{

	int maxn = 0;
	for (int i = 1; i <= 10000; i++)
	{
		for (int j = 2; j <= 15; j++)
		{
			int c = 0;
			if (pan_mul(i, j, c))
			{
				if (c > maxn)
				{
					//cout << i << " " << j << " " << c << endl;
					maxn = c;
				}
			}
		}
	}
	cout << maxn << endl;
	system("pause");
	return 0;
}
时间: 2024-11-05 16:09:50

Project Euler:Problem 38 Pandigital multiples的相关文章

Project Euler:Problem 52 Permuted multiples

It can be seen that the number, 125874, and its double, 251748, contain exactly the same digits, but in a different order. Find the smallest positive integer, x, such that 2x, 3x, 4x, 5x, and 6x, contain the same digits. #include <iostream> #include

Project Euler:Problem 41 Pandigital prime

We shall say that an n-digit number is pandigital if it makes use of all the digits 1 to n exactly once. For example, 2143 is a 4-digit pandigital and is also prime. What is the largest n-digit pandigital prime that exists? #include <iostream> #incl

Project Euler:Problem 32 Pandigital products

We shall say that an n-digit number is pandigital if it makes use of all the digits 1 to n 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 mult

Project Euler:Problem 46 Goldbach&#39;s other conjecture

It was proposed by Christian Goldbach that every odd composite number can be written as the sum of a prime and twice a square. 9 = 7 + 2×12 15 = 7 + 2×22 21 = 3 + 2×32 25 = 7 + 2×32 27 = 19 + 2×22 33 = 31 + 2×12 It turns out that the conjecture was f

Project Euler:Problem 40 Champernowne&#39;s constant

An irrational decimal fraction is created by concatenating the positive integers: 0.123456789101112131415161718192021... It can be seen that the 12th digit of the fractional part is 1. If dn represents the nth digit of the fractional part, find the v

Project Euler:Problem 18 Maximum path sum I

By starting at the top of the triangle below and moving to adjacent numbers on the row below, the maximum total from top to bottom is 23. 3 7 4 2 4 6 8 5 9 3 That is, 3 + 7 + 4 + 9 = 23. Find the maximum total from top to bottom of the triangle below

Project Euler:Problem 90 Cube digit pairs

Each of the six faces on a cube has a different digit (0 to 9) written on it; the same is done to a second cube. By placing the two cubes side-by-side in different positions we can form a variety of 2-digit numbers. For example, the square number 64

Project Euler:Problem 89 Roman numerals

For a number written in Roman numerals to be considered valid there are basic rules which must be followed. Even though the rules allow some numbers to be expressed in more than one way there is always a "best" way of writing a particular number

Project Euler:Problem 58 Spiral primes

Starting with 1 and spiralling anticlockwise in the following way, a square spiral with side length 7 is formed. 37 36 35 34 33 32 31 38 17 16 15 14 13 30 39 18  5  4  3 12 29 40 19  6  1  2 11 28 41 20  7  8  9 10 27 42 21 22 23 24 25 26 43 44 45 46