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

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

Humble Numbers

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)

Total Submission(s): 12573    Accepted Submission(s): 5509

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.
 

 

Source
 

 

Recommend
JGShining
 

分析:

只能含有素数因子2或3或5或7的数为Humble Numbers,
则可知:Humble Numbers的2倍或者3倍或者5倍或者7倍仍然是Humble Numbers,所以依次在当前找到的Humble Numbers中
依次*2;*3;*5;*7依次找最小的向后递推。

网上说这是经典的DP-----状态转移方程:

 ans(k) = min(ans(m)*2, ans(n)*3, ansF(p)*5, ans(q)*7)  (k > m,n,p,q)
                                                                                  特别的: m,n,p,q 只有在本项被选中后才移动
我感觉这题就是利用数组的滚动递推推出来的。。。。。

1 #include 
2 #include
3 4 using namespace std; 5 6 int ans[5900]; 7 8 int min(int a, int b) 9 {10 return a < b ? a : b;11 }12 13 void init()14 {15 ans[1] = 1;16 int m, n, p, q;17 m = n = p = q = 1;18 for(int i = 2; i <= 5842; ++i) // 题目要求只能含有素数因子2或3或5或7,所以只能用Humble Numbers为基数去*2;*3;*5或*719 {20 ans[i] = min(ans[m]*2, min(ans[n]*3, min(ans[p]*5, ans[q]*7)));21 if(ans[i] == ans[m] * 2) // 注意四个 if 是并列的,可能存在重叠的情况,此时都要相应的增加,否则结果会重复22 ++m;23 if(ans[i] == ans[n] * 3)24 ++n;25 if(ans[i] == ans[p] * 5)26 ++p;27 if(ans[i] == ans[q] * 7)28 ++q;29 }30 }31 32 void output(int n) // 这题打印结果要用到英文,很恶心人!33 {34 printf("The %d", n);35 int last = n%100;36 if(last==13 || last==12 || last==11)37 {38 printf("th humble number is %d.\n", ans[n]);39 return ;40 }41 last = n%10;42 if(last == 1)43 printf("st");44 else if(last == 2)45 printf("nd");46 else if(last == 3)47 printf("rd");48 else49 printf("th");50 printf(" humble number is %d.\n", ans[n]);51 }52 53 54 55 int main()56 {57 int n;58 init();59 while(scanf("%d", &n), n)60 {61 output(n);62 }63 return 0;64 }

 

转载地址:http://gjmpx.baihongyu.com/

你可能感兴趣的文章
问答项目---用户注册的那些事儿(JS验证)
查看>>
Android进阶篇-百度地图获取地理信息
查看>>
返回前一页并刷新页面方法
查看>>
2.3 InnoDB 体系架构
查看>>
linux系统配置之单一网卡配置多个不同网段IP(centos)
查看>>
.erb 中不能显示从mysql检索出的中文 incompatible character encodings: UTF-8 and ASCII-8BIT...
查看>>
51nod 1831: 小C的游戏(Bash博弈 找规律)
查看>>
使用filezilla连接树莓派失败
查看>>
[数分提高]2014-2015-2第5教学周第2次课讲义 3.2 微分中值定理
查看>>
Clr静态数据Table-Valued函数
查看>>
转:一个基于互联网医疗的创业公司,三年是一个收获
查看>>
How to effectively work with multiple files in Vim?
查看>>
Android 中文API (70) —— BluetoothDevice[蓝牙]
查看>>
不定宽高垂直居中分析
查看>>
ibatis中使用like模糊查询
查看>>
Scrum三头猪
查看>>
mysql之视图
查看>>
项目管理学习笔记之二.工作分解
查看>>
奇异值分解(We Recommend a Singular Value Decomposition)
查看>>
一个单元测试 学习 aysnc await
查看>>