Problem-D

概述:输出总长度小于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;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值