POJ 2247 Humble Numbers G++

本文介绍了一种计算Humble数的方法,并提供了一个C++程序实现。Humble数是指仅由2、3、5、7这四个质数作为因子的正整数,通过四层循环遍历所有可能的组合,将符合条件的数存储起来并排序,最后输出第N个Humble数。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

只由因子2,3,5,7组成的数称为humble数,所有humble数按升序排列。求第n个humble数。



#include <iostream>
#include <cmath>
#include <algorithm>
#include <vector>
//#include <iomanip>
using namespace std;
//谢谢博友文章 
vector<long long> hs;
int main()
{
	//cout<<pow(2,31)<<endl;
	//cout<<pow(3,20)<<endl;
	//cout<<pow(5,14)<<endl;
	//cout<<pow(7,12)<<endl;
	//cout<< setprecision(5)<<scientific<<2000000000<<endl;
	for(int a=0;a<32;a++)
	{
		for(int b=0;b<21;b++)
		{
			for(int c=0;c<15;c++)
			{
				for(int d=0;d<13;d++)
				{
					long long t=pow(2,a)*pow(3,b)*pow(5,c)*pow(7,d);
					if((t<2000000001)&&(t>0))
					{
						hs.push_back(t);
					}					
				}
			}
		}
	}
	sort(hs.begin(),hs.end());
	
	while(1)
	{
		int th;
		cin>>th;
		if(th==0)
		{
			break;
		}
		//谢谢博友 十位数字为1的都为th,其余情况下看各位数字,各位数字1, 2, 3分别对应st, nd, rd,其余也为th
		if((th%10==1)&&(th%100!=11))
		{
			cout<<"The "<<th<<"st humble number is "<<hs[th-1]<<"."<<endl;
		}else if((th%10==2)&&(th%100!=12))
		{
			cout<<"The "<<th<<"nd humble number is "<<hs[th-1]<<"."<<endl;
		}else if((th%10==3)&&(th%100!=13))
		{
			cout<<"The "<<th<<"rd humble number is "<<hs[th-1]<<"."<<endl;
		}else
		{
			cout<<"The "<<th<<"th humble number is "<<hs[th-1]<<"."<<endl;
		}				
	}
	return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值