码迷,mamicode.com
首页 > 其他好文 > 详细

Project Euler:Problem 62 Cubic permutations

时间:2015-07-14 18:21:53      阅读:113      评论:0      收藏:0      [点我收藏+]

标签:c++   project euler   

The cube, 41063625 (3453), can be permuted to produce two other cubes: 56623104 (3843) and 66430125 (4053). In fact, 41063625 is the smallest cube which has exactly three permutations of its digits which are also cube.

Find the smallest cube for which exactly five permutations of its digits are cube.



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

vector<int> sh(unsigned long long n)
{
	vector<int>res(10);
	while (n)
	{
		res[n % 10]++;
		n /= 10;
	}
	return res;
}

int main()
{
	map<vector<int>, vector<int>>mp;
	for (int i = 1; i < 10000; i++)
	{
		unsigned long long n = i*i;
		n = n*i;
		vector<int>tmp(10);
		tmp= sh(n);
		//cout << i << endl;
		if (mp.find(tmp) == mp.end())
			mp[tmp] = { i };
		else
			mp[tmp].push_back(i);


	}
	map<vector<int>, vector<int>>::iterator iter;
	for (iter = mp.begin(); iter != mp.end(); iter++)
	{
		if (iter->second.size() == 5)
		{
			for (int i = 0; i < 5; i++)
			{
				cout << iter->second[i] <<" ";
			}
			unsigned long long n = iter->second[0] * iter->second[0];
			n = n*iter->second[0];
			cout << n << endl;
		}
	}

	system("pause");
	return 0;
}

有两组结果,结果为首项最小的那个。

版权声明:本文为博主原创文章,未经博主允许不得转载。

Project Euler:Problem 62 Cubic permutations

标签:c++   project euler   

原文地址:http://blog.csdn.net/youb11/article/details/46878509

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!