Humble Numbers soj1029

简介:

         Humble Numbers

A number whose only prime factors are 2,3,5 or 7 is called a humble number. The sequence 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 12, 14, 15, 16, 18, 20, 21, 24, 25, 27, ... shows the first 20 humble numbers.

Write a program to find and print the nth element in this sequence.

Input Specification

The input consists of one or more test cases. Each test case consists of one integer n with tex2html_wrap_inline35 . Input is terminated by a value of zero (0) for n.

Output Specification

For each test case, print one line saying "The nth humble number is number.". Depending on the value of n, the correct suffix "st", "nd", "rd", or "th" for the ordinal number nth has to be used like it is shown in the sample output.

Sample Input

1
2
3
4
11
12
13
21
22
23
100
1000
5842
0

Sample Output

The 1st humble number is 1.
The 2nd humble number is 2.
The 3rd humble number is 3.
The 4th humble number is 4.
The 11th humble number is 12.
The 12th humble number is 14.
The 13th humble number is 15.
The 21st humble number is 28.
The 22nd humble number is 30.
The 23rd humble number is 32.
The 100th humble number is 450.
The 1000th humble number is 385875.
The 5842nd humble number is 2000000000.

  题意:求出丑数。所谓的丑数就是因子只含2,3,5,7.若一个数n是丑数,则必定有n=2^a*3^b*5^c*7^d,因此只需求出所有的丑数,对其进行排序即可。

 在这里还有注意输出格式的问题,对于序数词:

 1)若n%10=1,2,3并且n%100!=11,12,13,则末尾为st,nd,rd;

 2)否则末尾为th;

复制代码
#include < iostream >
#include
< algorithm >
#include
< stdlib.h >
#include
< math.h >
#define  MAX 2000000000
using namespace  std;

int  hum[ 5842 ];

void  getHum()
{
int  a,b,c,d;
int  len = 0 ;
long long  num2,num3,num5,num7; 
for (a = 0 ;a <= 31 ;a ++ )
{
num2
= ( long long )pow( 2.0 ,( double )a);
for (b = 0 ;b <= 20 ;b ++ )
{
num3
= ( long long )pow( 3.0 ,( double )b);
if (num2 * num3 > MAX)
break ;
for (c = 0 ;c <= 14 ;c ++ )
{
num5
= ( long long )pow( 5.0 ,( double )c);
if (num2 * num3 * num5 > MAX)
break ;
for (d = 0 ;d <= 12 ;d ++ )

num7
= ( long long )pow( 7.0 ,( double )d);
if (num2 * num3 * num5 * num7 <= MAX)
hum[len
++ ] = num2 * num3 * num5 * num7;
else break ;
}
}
}
}
sort(hum,hum
+ len);
}



int  main( void )
{
int  n;
getHum();
while (scanf( " %d " , & n) == 1 && n != 0 )
{
if (n % 10 == 1 && n % 100 != 11 )
printf(
" The %dst humble number is %d.\n " ,n,hum[n - 1 ]);
else if (n % 10 == 2 && n % 100 != 12 )
printf(
" The %dnd humble number is %d.\n " ,n,hum[n - 1 ]);
else if (n % 10 == 3 && n % 100 != 13 )
printf(
" The %drd humble number is %d.\n " ,n,hum[n - 1 ]);
else
printf(
" The %dth humble number is %d.\n " ,n,hum[n - 1 ]);
}
return 0 ;
}


本文转载自海 子博客园博客,原文链接:http://www.cnblogs.com/dolphin0520/archive/2011/04/15/2016774.html如需转载自行联系原作者

相关文章
|
3天前
|
关系型数据库 Serverless 分布式数据库
高峰无忧,探索PolarDB PG版Serverless的弹性魅力
在数字经济时代,数据库成为企业命脉,面对爆炸式增长的数据,企业面临管理挑战。云原生和Serverless技术革新数据库领域,PolarDB PG Serverless作为阿里云的云原生数据库解决方案,融合Serverless与PostgreSQL,实现自动弹性扩展,按需计费,降低运维成本。它通过计算与存储分离技术,提供高可用性、灾备策略和简化运维。PolarDB PG Serverless智能应变业务峰值,实时监控与调整资源,确保性能稳定。通过免费体验,用户可观察其弹性性能和价格力,感受技术优势。
|
12天前
|
Kubernetes 安全 Devops
【云效流水线 Flow 测评】驾驭云海:五大场景下的云效Flow实战部署评测
云效是一款企业级持续集成和持续交付工具,提供免费、高可用的服务,集成阿里云多种服务,支持蓝绿、分批、金丝雀等发布策略。其亮点包括快速定位问题、节省维护成本、丰富的企业级特性及与团队协作的契合。基础版和高级版分别针对小型企业和大规模团队,提供不同功能和服务。此外,云效对比Jenkins在集成阿里云服务和易用性上有优势。通过实战演示了云效在ECS和K8s上的快速部署流程,以及代码质量检测和AI智能排查功能,展示了其在DevOps流程中的高效和便捷,适合不同规模的企业使用。本文撰写用时5小时,请各位看官帮忙多多支持,如有建议也请一并给出,您的建议能帮助我下一篇更加出色。
136109 16
|
13天前
|
存储 缓存 监控
你的Redis真的变慢了吗?性能优化如何做
本文先讲述了Redis变慢的判别方法,后面讲述了如何提升性能。
102161 2
|
13天前
|
机器学习/深度学习 并行计算 算法
Transformer 一起动手编码学原理
学习Transformer,快来跟着作者动手写一个。
94233 5
|
12天前
|
存储 SQL Apache
阿里云数据库内核 Apache Doris 基于 Workload Group 的负载隔离能力解读
阿里云数据库内核 Apache Doris 基于 Workload Group 的负载隔离能力解读
阿里云数据库内核 Apache Doris 基于 Workload Group 的负载隔离能力解读
|
18天前
|
人工智能 弹性计算 算法
一文解读:阿里云AI基础设施的演进与挑战
对于如何更好地释放云上性能助力AIGC应用创新?“阿里云弹性计算为云上客户提供了ECS GPU DeepGPU增强工具包,帮助用户在云上高效地构建AI训练和AI推理基础设施,从而提高算力利用效率。”李鹏介绍到。目前,阿里云ECS DeepGPU已经帮助众多客户实现性能的大幅提升。其中,LLM微调训练场景下性能最高可提升80%,Stable Difussion推理场景下性能最高可提升60%。
|
13天前
|
存储 弹性计算 Cloud Native
1 名工程师轻松管理 20 个工作流,创业企业用 Serverless 让数据处理流程提效
为应对挑战,语势科技采用云工作流CloudFlow和函数计算FC,实现数据处理流程的高效管理与弹性伸缩,提升整体研发效能。
64688 2
|
20天前
|
消息中间件 安全 API
Apache RocketMQ ACL 2.0 全新升级
RocketMQ ACL 2.0 不管是在模型设计、可扩展性方面,还是安全性和性能方面都进行了全新的升级。旨在能够为用户提供精细化的访问控制,同时,简化权限的配置流程。欢迎大家尝试体验新版本,并应用在生产环境中。
187462 6
|
16天前
|
存储 关系型数据库 数据库
|
23天前
|
物联网 PyTorch 测试技术
手把手教你捏一个自己的Agent
Modelscope AgentFabric是一个基于ModelScope-Agent的交互式智能体应用,用于方便地创建针对各种现实应用量身定制智能体,目前已经在生产级别落地。