博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
humble number(hd1058)
阅读量:5256 次
发布时间:2019-06-14

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

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.
1 /*看题解AC的,结果开始开编译错误了几次,oj说是min()没定义,那为什么自己编译会通过,然后自己写了min(),dp方程 dp[i]=f[i]=min(f[a]*2,min(f[b]*3,min(f[c]*5,f[d]*7)))*/ 2 #include
/*dev编译后运行程序中途闪退,提交后却对的*/ 3 using namespace std; 4 int min(int a,int b) 5 { 6 return a>b?b:a; 7 } 8 int humble[5843]; 9 void factor()/*马丹看了半天也是似懂非懂,比赛时完全想不出来这么求*/10 {11 int post1=1,post2=1,post3=1,post4=1,na,nb,nc,nd,i;12 humble[1]=1;13 for(i=2;i<=5843;i++)14 {15 humble[i]=min(na=humble[post1]*2,min(nb=humble[post2]*3,min(nc=humble[post3]*5,nd=humble[post4]*7)));16 if(humble[i]==na) post1++;17 if(humble[i]==nb) post2++;18 if(humble[i]==nc) post3++;19 if(humble[i]==nd) post4++;20 }21 }22 int main()23 {24 int n;25 factor();26 while(~scanf("%d",&n))27 {28 if(n==0)29 break;30 printf("The %d",n);31 if(n%100!=11&&n%10==1)32 printf("st");33 else if(n%100!=12&&n%10==2)34 printf("nd");35 else if(n%100!=13&&n%10==3)36 printf("rd");37 else38 printf("th");39 printf(" humble number is %d.\n",humble[n]);40 } 41 }

 

转载于:https://www.cnblogs.com/a1225234/p/4498976.html

你可能感兴趣的文章
[leetcode]Longest Valid Parentheses
查看>>
vue 子组件调用父组件数据
查看>>
A页面跳转到B页面后打开指定tabs标签
查看>>
[leedcode 16] 3Sum Closest
查看>>
trigger()的event事件对象之坑
查看>>
用 conky 记录 TODO
查看>>
使用XQuery的nodes()方法实现字符拆分
查看>>
标准C程序设计七---14
查看>>
Linux汇编教程04:寻址方式
查看>>
编写更好的jQuery代码
查看>>
Altera Quartus II 12.0订购版下载
查看>>
DON'T SET YOUR SHAREPOINT APP TO FULL TRUST
查看>>
移动端踩坑系列(一)————input默认调用数字键盘
查看>>
第十八次ScrumMeeting会议
查看>>
mysql部分替换
查看>>
IOS关于数据加密(主要为登录加密)想总结的
查看>>
特征脸是怎么提取的之主成分分析法PCA
查看>>
[转]IE8兼容Jquery.validate.js兼容问题
查看>>
摄影基本知识
查看>>
Nginx Web服务器配置
查看>>