练习三 1004

概述:有一个数列,它的成员都是2,3,5,7的倍数,输入一个n,求出这个数列的第n个数。

思路:先列一个表,把这些数据全求出来,利用循环,设a,b,c,d=1,分别对他们*2,*3,*5,*7,取最小值入表,并将其+1,依次类推,最后输出所求值即可。

感想:以前做过的题,拿来想想就有思路了。

#include <iostream>
#include <cstdio>
using namespace std;
typedef long long LL;
LL dp[5843];
LL min(LL a, LL b, LL c, LL d)
{
	LL temp1 = a < b ? a : b;
	temp1 = temp1 < c ? temp1 : c;
	temp1 = temp1 < d ? temp1 : d;
	return temp1;
}
int main()
{
	
	dp[1] = 1;
	int a = 1, b = 1, c = 1, d = 1;
	for (int i = 2; i <= 5842; ++i)
	{
		dp[i] = min(dp[a] * 2, dp[b] * 3, dp[c] * 5, dp[d] * 7);
		if (dp[i] == dp[a] * 2) a++;
		if (dp[i] == dp[b] * 3) b++;
		if (dp[i] == dp[c] * 5) c++;
		if (dp[i] == dp[d] * 7) d++;
	}
	int n;
	while (cin>>n&&n)
	{
		if (n % 100 != 11 && n % 10 == 1)printf("The %dst humble number is %lld.\n", n, dp[n]);
		else
			if (n % 100 != 12 && n % 10 == 2)printf("The %dnd humble number is %lld.\n", n, dp[n]);
			else
				if (n % 100 != 13 && n % 10 == 3)printf("The %drd humble number is %lld.\n", n, dp[n]);
				else
					printf("The %dth humble number is %lld.\n", n, dp[n]);
	}
	return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值