博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
hdu1058(dp)
阅读量:6995 次
发布时间:2019-06-27

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

Humble Numbers

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

Total Submission(s): 10640    Accepted Submission(s): 4622

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
 
简单DP,可惜在校赛的时候没能AC。本题唯一要注意的是st,nd,rd,th的输出时机。特别是n=11,12,13的时候。
View Code
#include
using namespace std;int humble[5843];void DP(){ humble[1]=1; int pos1,pos2,pos3,pos4; __int64 a,b,c,d; pos1=pos2=pos3=pos4=1; int i; for(i=2;i<5843;i++) { a=humble[pos1]*2; b=humble[pos2]*3; c=humble[pos3]*5; d=humble[pos4]*7; humble[i]=min(min(a,b),min(c,d)); if(humble[i]==a)pos1++; if(humble[i]==b)pos2++; if(humble[i]==c)pos3++; if(humble[i]==d)pos4++; }}int main(){// freopen("test.txt","r",stdin); DP(); int n; 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",humble[n]); } return 0;}

 

转载于:https://www.cnblogs.com/xiuyangleiasp/archive/2012/11/20/2778906.html

你可能感兴趣的文章
第三次作业-结对编程
查看>>
文件上传,跨浏览器统一的样式
查看>>
内存屏障
查看>>
Cocos2d提供的字体
查看>>
(一)html之基本结构
查看>>
bootstrop
查看>>
文字过长隐藏
查看>>
LeetCode OJ - Minimum && Maximum Depth of Binary Tree
查看>>
如何将Linux rm命令删除的文件放入垃圾箱
查看>>
引用MinGW生成的.dll.a后出现的问题
查看>>
51Nod1130斯特林近似
查看>>
dede 调用原图的路径
查看>>
浅析设计模式(四)——建造者模式
查看>>
PAT 1081 Rational Sum[分子求和][比较]
查看>>
PAT 1039 Course List for Student[难]
查看>>
BZOJ 4145: [AMPPZ2014]The Prices
查看>>
软件测试面试必问题及答案
查看>>
Flex 4 权威指南 学习笔记
查看>>
细说虚拟机栈
查看>>
以交互方式将文本添加到图形中(matlab)
查看>>