博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
HDU 1058 Humble Numbers
阅读量:6578 次
发布时间:2019-06-24

本文共 2020 字,大约阅读时间需要 6 分钟。

Problem Description
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
The input consists of one or more test cases. Each test case consists of one integer n with 1 <= n <= 5842. Input is terminated by a value of zero (0) for n.
Output
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.
分析:    如果一个数是Humble Number,那么它的2倍,3倍,5倍,7倍仍然是Humble Number
       定义F[i]为第i个Humble Number
       F[n]=min(2*f[i],3*f[j],5*f[k],7*f[L]), i,j,k,L在被选择后相互移动
      (通过此题理解到数组有序特性)   
code :
View Code
#include
#define min(a,b)((a)<(b))?(a):(b) int main() {
int i,a,b,c,d,n; int hum[5850]; hum[1]=1; a=b=c=d=1; for(i=2;i<=5842;i++) {
hum[i]=min(hum[a]*2,min(hum[b]*3,min(hum[c]*5,hum[d]*7))); if(hum[i]==hum[a]*2) a++; if(hum[i]==hum[b]*3) b++; if(hum[i]==hum[c]*5) c++; if(hum[i]==hum[d]*7) d++; } while(scanf("%d",&n),n) {
printf("The %d",n); if(n%100!=11&&n%10==1)printf("st"); else if(n%100!=12&&n%10==2)printf("nd"); else if(n%100!=13&&n%10==3)printf("rd"); else printf("th"); printf(" humble number is %d.\n",hum[n]); } return 0; }

转载于:https://www.cnblogs.com/dream-wind/archive/2012/03/14/2396345.html

你可能感兴趣的文章
产品研发项目管理软件哪个好?
查看>>
【阿里云北京峰会】一图看懂机器学习PAI如何帮助企业应用智能化升级
查看>>
ansible playbook使用总结
查看>>
Android API中文文档(111) —— MailTo
查看>>
Linux 中如何卸载已安装的软件
查看>>
thinkphp 3.2 增加每页显示条数
查看>>
oracle日常简单数据备份与还原
查看>>
我的友情链接
查看>>
黑马程序员__反射总结
查看>>
Scala学习笔记(5)-类和方法
查看>>
Quartz原理
查看>>
完全卸载oracle|oracle卸载|彻底卸载oracle
查看>>
垃圾收集基础
查看>>
Docker安装及基本命令
查看>>
控制namenode检查点发生的频率
查看>>
Linux存储挂载后,无法正常卸载的解决方法
查看>>
2、递归遍历文件夹下每一个文件
查看>>
Remove auto_increment from Schema Dumps (mysqld...
查看>>
解决activity加上Theme.Translucent.NoTitleBar 页面跳转显示桌面
查看>>
php类库
查看>>