【ProjectEuler】ProjectEuler_052(找出最小的正整数x,使得2x, 3x, 4x, 5x和6x都包含同样的数字)
2013-04-21 18:26
459 查看
#pragma once #include <windows.h> #include <vector> #include <set> using namespace std; class MoonMath { public: MoonMath(void); ~MoonMath(void); //************************************ // Method: IsInt // Access: public // Describe: 判断double值在epsilon的范围内是否很接近整数 // 如1.00005在epsilon为0.00005以上就很接近整数 // Parameter: double doubleValue 要判断的double值 // Parameter: double epsilon 判断的精度,0 < epsilon < 0.5 // Parameter: INT32 & intValue 如果接近,返回最接近的整数值 // Returns: bool 接近返回true,否则返回false //************************************ static bool IsInt(double doubleValue, double epsilon, INT32 &intValue); //************************************ // Method: Sign // Access: public // Describe: 获取value的符号 // Parameter: T value 要获取符号的值 // Returns: INT32 正数、0和负数分别返回1、0和-1 //************************************ template <typename T> static INT32 Sign(T value); const static UINT32 MIN_PRIMER = 2; // 最小的素数 //************************************ // Method: IsPrimer // Access: public // Describe: 判断一个数是否是素数 // Parameter: UINT32 num 要判断的数 // Returns: bool 是素数返回true,否则返回false //************************************ static bool IsPrimer(UINT32 num); //************************************ // Method: IsIntegerSquare // Access: public static // Describe: 判断给定的数开平方后是否为整数 // Parameter: UINT32 num // Returns: bool //************************************ static bool IsIntegerSquare(UINT32 num); //************************************ // Method: GetDiffPrimerFactorNum // Access: public static // Describe: 获取num所有的不同质因数 // Parameter: UINT32 num // Returns: set<UINT32> //************************************ static set<UINT32> MoonMath::GetDiffPrimerFactorNum(UINT32 num); //************************************ // Method: GetDigitMap // Access: public // Describe: 获取num包含的数字map // Parameter: UINT32 num // Parameter: UINT16 & digitMap 返回的num的数字map,为2进制 // Returns: bool 如果包含重复的数字,返回false,否则返回true //************************************ static bool GetDigitMap(UINT32 num, UINT16 &digitMap); //************************************ // Method: IsSameDigitNum // Access: public // Describe: 判断N个数字是否都是由相同的数字组成,每个数字都必须包含不同的数字 // Parameter: const UINT32 nums[] N个数字组成的数组 // Parameter: UINT32 numCount 数字个数 // Returns: bool //************************************ static bool IsNumsHaveSameDigit(const UINT32 nums[], UINT32 numCount); //************************************ // Method: Factorial // Access: public // Describe: 获得n的阶乘 // Parameter: UINT32 n // Returns: UINT32 //************************************ static UINT32 Factorial(UINT32 n); //************************************ // Method: Combination // Access: public // Describe: 求(n,r)的组合 // Parameter: UINT32 n // Parameter: UINT32 r // Returns: UINT32 //************************************ static UINT32 Combination(UINT32 n,UINT32 r); };
#include "MoonMath.h" #include <cmath> #include <iostream> using namespace std; MoonMath::MoonMath(void) { } MoonMath::~MoonMath(void) { } template <typename T> INT32 MoonMath::Sign(T value) { if(value > 0) { return 1; } else if(value == 0) { return 0; } else { return -1; } } bool MoonMath::IsInt(double doubleValue, double epsilon, INT32 &intValue) { if(epsilon > 0.5 || epsilon < 0) { return false; } if(INT32(doubleValue + epsilon) == INT32(doubleValue - epsilon)) { return false; } INT32 value = INT32(doubleValue); intValue = (fabs(doubleValue - value) > 0.5) ? (value + MoonMath::Sign(doubleValue)) : (value) ; return true; } bool MoonMath::IsPrimer(UINT32 num) { if(num < MIN_PRIMER) { return false; } if(num == MIN_PRIMER) { return true; } // 判断是否能被2整除 if((num & 1) == 0) { return false; } UINT32 sqrtOfNum = (UINT32)sqrt((double)num); // num的2次方 // 从MIN_PRIMER到sqrt(num),如果任何数都不能被num整除,num是素数,否则不是 for(UINT32 i = MIN_PRIMER + 1; i <= sqrtOfNum; i += 2) { if(num % i == 0) { return false; } } return true; } bool MoonMath::IsIntegerSquare(UINT32 num) { UINT32 qurtNum = (UINT32)sqrt((double)num); return (qurtNum * qurtNum) == num; } set<UINT32> MoonMath::GetDiffPrimerFactorNum(UINT32 num) { UINT32 halfNum = num / 2; set<UINT32> factors; for(UINT32 i = 2; i <= halfNum; ++i) { if(!MoonMath::IsPrimer(i)) { continue; } if(num % i == 0) { factors.insert(i); while(num % i == 0) { num /= i; } } } return factors; } bool MoonMath::GetDigitMap(UINT32 num, UINT16 &digitMap) { UINT32 digit = 0; digitMap = 0; while(num != 0) { digit = num % 10; num /= 10; // 数字已存在,返回false if(digitMap & (1 << digit)) { return false; } digitMap |= (1 << digit); } return true; } bool MoonMath::IsNumsHaveSameDigit(const UINT32 nums[], UINT32 numCount) { UINT16 lastDigitMap = 0; UINT16 currDigitMap = 0; if(numCount < 2) { return false; } if(!MoonMath::GetDigitMap(nums[0], lastDigitMap)) { return false; } for(UINT32 i = 1; i < numCount; ++i) { if(!MoonMath::GetDigitMap(nums[i], currDigitMap)) { return false; } if(currDigitMap != lastDigitMap) { return false; } } return true; } UINT32 MoonMath::Factorial(UINT32 n) { if(n <= 1) { return 1; } UINT32 result = 1; for(UINT32 i = 2; i <= n; ++i) { result *= i; } if (result==0) { cout<<n<<endl; } return result; } UINT32 MoonMath::Combination(UINT32 n, UINT32 r) { // C(n,r)=n!/r!/(n-r)!=(r+1)*(r+2)*...*n/1/2/.../(n-r) UINT32 result=1; for (UINT32 i=r+1;i<=n;++i) { result*=i; } UINT32 d=n-r; for (UINT32 i=2;i<=d;++i) { result/=i; } return result; }
// Permuted multiples // Problem 52 // 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. // // 题目52:找出最小的正整数x,使得2x, 3x, 4x, 5x和6x都包含同样的数字。 // 125874和它的二倍,251748, 包含着同样的数字,只是顺序不同。 // // 找出最小的正整数x,使得 2x, 3x, 4x, 5x, 和6x都包含同样的数字。 #include <iostream> #include <windows.h> #include <ctime> #include <assert.h> #include <MoonMath.h> using namespace std; // 打印时间等相关信息 class DetailPrinter { public: void Start(); void End(); DetailPrinter(); private: LARGE_INTEGER timeStart; LARGE_INTEGER timeEnd; LARGE_INTEGER freq; }; DetailPrinter::DetailPrinter() { QueryPerformanceFrequency(&freq); } //************************************ // Method: Start // Access: public // Describe: 执行每个方法前调用 // Returns: void //************************************ void DetailPrinter::Start() { QueryPerformanceCounter(&timeStart); } //************************************ // Method: End // Access: public // Describe: 执行每个方法后调用 // Returns: void //************************************ void DetailPrinter::End() { QueryPerformanceCounter(&timeEnd); cout << "Total Milliseconds is " << (double)(timeEnd.QuadPart - timeStart.QuadPart) * 1000 / freq.QuadPart << endl; const char BEEP_CHAR = '\007'; cout << endl << "By GodMoon" << endl << __TIMESTAMP__ << BEEP_CHAR << endl; system("pause"); } /*************************解题开始*********************************/ //************************************ // Method: GetMulNum // Access: public // Describe: 获取num的倍数 // Parameter: UINT32 num // Parameter: UINT32 mulNum[] 倍数存储位置,保存mulNumCount+1个num的倍数 // Parameter: UINT32 mulNumCount 最大倍数 // Returns: void //************************************ void GetMulNum(UINT32 num, UINT32 mulNum[], UINT32 mulNumCount) { for(UINT32 i = 1; i <= mulNumCount; ++i) { mulNum[i - 1] = num * i; } } void TestFun1() { cout << "TestFun1 OK!" << endl; } void F1() { cout << "void F1()" << endl; // TestFun1(); DetailPrinter detailPrinter; detailPrinter.Start(); /*********************************算法开始*******************************/ const UINT32 NUM_COUNT = 6; UINT32 mulNums[NUM_COUNT]; UINT32 num; for(num = 1;; ++num) { GetMulNum(num, mulNums, ARRAYSIZE(mulNums)); if(MoonMath::IsNumsHaveSameDigit(mulNums, ARRAYSIZE(mulNums))) { break; } } cout << "The smallest num is " << num << endl; /*********************************算法结束*******************************/ detailPrinter.End(); } //主函数 int main() { F1(); return 0; } /* void F1() The smallest num is 142857 Total Milliseconds is 32.823 By GodMoon Thu Mar 28 22:46:09 2013 */
相关文章推荐
- 美团点评在线笔试题:在只包含'0'-'9'的字符串中找出无法用字符串中数字表示的最小正整数
- 输入n个整数,找出其中最小的K个数。例如输入4,5,1,6,2,7,3,8这8个数字,则最小的4个数字是1,2,3,4,。
- 谷歌笔试题--给定一个集合A=[0,1,3,8](该集合中的元素都是在0,9之间的数字,但未必全部包含), 指定任意一个正整数K,请用A中的元素组成一个大于K的最小正整数。
- 谷歌笔试题--给定一个集合A=[0,1,3,8](该集合中的元素都是在0,9之间的数字,但未必全部包含), 指定任意一个正整数K,请用A中的元素组成一个大于K的最小正整数。
- 算法--找出与目标数字相同的字符组成的整数中比该数字大的数集中的最小数字
- 输入n个整数,找出其中最小的K个数。例如输入4,5,1,6,2,7,3,8这8个数字,则最小的4个数字是1,2,3,4,。
- 输入n个整数,找出其中最小的K个数。例如输入4,5,1,6,2,7,3,8这8个数字,则最小的4个数字是1,2,3,4,。
- 1、 输入一串字符,只包含“0-10”和“,”找出其中最小的数字和最大的数字(可能不止一个),输出最后剩余数字个数。
- 谷歌笔试题--给定一个集合A=[0,1,3,8](该集合中的元素都是在0,9之间的数字,但未必全部包含), 指定任意一个正整数K,请用A中的元素组成一个大于K的最小正整数。
- 有 n 个无序整数( n>10000), 则找出其中最大的 M 个数字( 5<M<10), 所需要的最小时间复杂度为:
- 陈利人 面试题 对于一个n位正整数a,去掉其中任意k(k<=n)个数字后,剩下的数字按原次序排列可以组成一个新的正整数。设计一个删数算法,使得剩下的数字组成的正整数最小。
- 给定K个整数,找出由这K个整数组成的最小的数字
- 输入一个字符串只包含“0~10”和”,“找出最小的数字和最大的数字(可能不止一个),输出最后剩余数字的个数,如输入”3,3,4,5,6,7,7“
- 给定一个整数,并允许将其删除n个数字,使得值最小,例:data:178593, 删除一个数字,result:17593
- 找出包含指定数字的整数并排序
- 输入n个整数,找出其中最小的K个数。例如输入4,5,1,6,2,7,3,8这8个数字,则最小的4个数字是1,2,3,4,。
- 在整数中删除数字,使得余下的数字按原次序组成的新数最小
- 在整数中删除数字,留下4个,使得余下的数字按原次序组成的新数最小
- 用堆实现最小的K个数的查找:输入n个整数,找出其中最小的K个数。例如输入4,5,1,6,2,7,3,8这8个数字,则最小的4个数字是1,2,3,4,。
- 在整数n=742683613984中删除8个数字,使得余下的数字按原次序组成的新数最小(不含0)