概述:输出总长度小于5842的数字,数字满足的规则是必须含有因子2,3,5,7。
思路:发现了一个强大的算法,就是四个数字,分别对应四个数字,每次分别乘2,3,5,7.然后这些数字求最小值,然后最小值对应的数字加,再次相乘,不断循环,直到到达极限。
感想:自己开始时候做了这个题,自己的思路是数字乘2,3,5,7,然后取恰当的值,忽然发现这样或许会有拉下的数字,然后用数组储存求出的数,分别对2,3,5,7相除,满足就是,但是这样占的内存比较大,最后发现了这个思路,惊为天人!!强大的想法。
#include<iostream>
#include<fstream>
#include<string.h>
#include<algorithm>
using namespace std;
int min(int a, int b)
{
if (a < b)
return a;
else return b;
}
int MIN(int a, int b, int c, int d)
{
return min(min(a, b), min(c, d));
}
int main()
{
//ifstream cin("in.txt");
int n = 1;
int a[5855] = { 0 };
int p2 = 1, p3 = 1, p5 = 1, p7 = 1;
a[1] = 1;
while (a[n] <= 2000000000)
{
a[++n] = MIN(2*a[p2],3*a[p3],5*a[p5],7*a[p7]);
if (a[n] == 2 * a[p2]) p2++;
if (a[n] == 3 * a[p3]) p3++;
if (a[n] == 5 * a[p5]) p5++;
if (a[n] == 7 * a[p7]) p7++;
}
while (cin>>n && n)
{
if (n % 10 == 1 && n % 100 != 11)
cout << "The "<<n<<"st humble number is " ;
else if (n % 10 == 2 && n % 100 != 12)
cout << "The "<<n<<"nd humble number is " ;
else if (n % 10 == 3 && n % 100 != 13)
cout << "The "<<n<<"rd humble number is " ;
else
cout << "The "<<n<<"th humble number is " ;
cout << a[n] << "." << endl;
}
return 0;
}