目录

Humble Numbers

解题思路:

ac代码:


Humble Numbers

Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 65536/32768K (Java/Other)

Total Submission(s) : 17   Accepted Submission(s) : 14

Font: Times New Roman | Verdana | Georgia

Font Size: ← →

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)筛法(详见代码1,标准模版)

2)for循环一次遍历(不推荐,见代码2),模拟一样就懂了

注意输出格式的问题

ac代码:


代码1

#include <iostream>
#include <cmath>
#include <algorithm>
#include <queue>
#include <cstring>
#define ll long long int
#define maxn 5890
using namespace std;
int main()
{ll a[maxn];int i,n;int f2=1,f3=1,f5=1,f7=1;a[1]=1;for(i=2;i<=maxn;i++){a[i]=min(a[f2]*2,min(a[f3]*3,min(a[f5]*5,a[f7]*7)));if(a[i]==a[f2]*2) f2++;if(a[i]==a[f3]*3) f3++;if(a[i]==a[f5]*5) f5++;if(a[i]==a[f7]*7) f7++;}while(scanf("%d",&n),n){if(n==0) break;printf("The %d",n);if(n%10==1 && n%100!=11) printf("st");else if(n%10==2 && n%100!=12) printf("nd");else if(n%10==3 && n%100!=13) printf("rd");else printf("th");printf(" humble number is %lld.\n",a[n]);}return 0;}

代码2

#include <iostream>
using namespace std;int main()
{unsigned long long humbleNums[5842] = { 0 };humbleNums[0] = 1;int prime[4] = { 2, 3, 5, 7 };for (unsigned long long i = 1; i < 5842; ++i){humbleNums[i] = 20000000001;for (int j = 0; j < 4; ++j){for (int k = i - 1; k >= 0;k--){if (humbleNums[k]*prime[j]<=humbleNums[i-1]){break;}if (humbleNums[k]*prime[j]<humbleNums[i]){humbleNums[i] = humbleNums[k] * prime[j];}}}}int n;while (cin >> n){if (n == 0){break;}cout << "The " << n;if (n % 10 == 1 && n % 100 != 11)cout << "st ";else if (n % 10 == 2 && n % 100 != 12)cout << "nd ";else if (n % 10 == 3 && n % 100 != 13)cout << "rd ";else  cout << "th ";cout << "humble number is " << humbleNums[n-1] << "." << endl;}return 0;
}---------------------本文来自 alw_123 的CSDN 博客 ,全文地址请点击:https://blog.csdn.net/alw_123/article/details/51674140?utm_source=copy 

hdoj1058:Humble Numbers(dp基础题+技巧筛法)相关推荐

  1. 树形dp瞎讲+树形dp基础题题解

    ---恢复内容开始--- 没错 咕了这么久(没有青青姐久 我又开始写博客了( ´▽`) 想了很久些什么(才没有想过呢 虽然被鄙视基础不好但还是走上了树形dp的不归路 那么 就来写写树形dp吧(dtx ...

  2. zoj1095 Humble Numbers(DP)

    /*  简单DP:每次将求得的值分别乘以2,3,4,7,然后利用插入排序插入到数据序列中    保证数据的递增序,最终到达结束状态. */ View Code 1 #include <iostr ...

  3. 牛客假日团队赛5 K 金币馅饼 (DP 基础题)

    链接:https://ac.nowcoder.com/acm/contest/984/K 来源:牛客网 金币馅饼 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 32768K,其他语言 ...

  4. UVA103 dp基础题,DAG模型

    1.UVA103 嵌套n维空间 DAG模型记忆化搜索,或者 最长上升子序列. 2.dp[i]=max( dp[j]+1),(第i个小于第j个) (1) //DAG模型记忆化搜索 #include< ...

  5. A. Boredom(线性dp基础题)

    题目的大意是:给定你一个数组,每次可以删掉一个大小为a的数,获得a的分数,同时删除数列中所有大小为a+1和a-1的数.问删除所有数组中的数后,能够获得的最大分数值. 思路:本题可以用线性dp来解决.d ...

  6. NUC1077 Humble Numbers【数学计算+打表+水题】

    Humble Numbers 时间限制: 1000ms 内存限制: 65536KB 通过次数: 1总提交次数: 1 问题描述 A number whose only prime factors are ...

  7. (HDU)1058 --Humble Numbers( 丑数)

    题目链接:http://vjudge.net/problem/HDU-1058 这题有点难度,自己写了半天依旧TLE,参考了其他人的博客. http://blog.csdn.net/pythonfx/ ...

  8. 51nod 基础题题解(全)

    基础题(40): 1000 A + B 1005 大数加法 1006 最长公共子序列Lcs 1018 排序 1019 逆序数 1027 大数乘法 1046 A^B Mod C 1057 N的阶乘(大数 ...

  9. python代码基础题-python第一部分基础题1-80题

    各位Python的第一部分1-80道基础题已经整理好了,希望面试的时候能用的上. 1.为什么学习Python? Python是目前市面上,我个人认为是最简洁.最优雅.最有前途.最全能的编程语言,没有之 ...

  10. HDU1058 Humble Numbers

    题目:Humble Numbers humble number从1为"始祖",剩下的所有数,其实都是在此基础上乘以2,3,5,7演化出来的,代码主要语句:f[t]=min(2*f[ ...

最新文章

  1. video processing on Mac and iOS
  2. 学了半天,import 到底在干啥?
  3. pytorch选出数据中的前k个最大(最小)值及其索引
  4. 通达信服务器维修点查询,通达信验证服务器数据库修改
  5. Android内存解析(二)— 详解内存,内部存储和外部存储
  6. 判断设置两天后时间,时间戳
  7. cesium 模型绕点飞行一周
  8. mybatis 之 parameterType=list
  9. DevOps Master凤凰项目沙盘总结:大美的DevOps总结
  10. 计算机网络原理视频学习教程
  11. CaysnPrinter - Windows平台开发包打印示例程序及接口说明文档 - 20170710
  12. mysql积分表和业务表_积分相关数据库表该如何设计?业务表和积分明细表该如何匹配上...
  13. java中时钟回拨,【追光者系列】HikariCP源码分析之evict、时钟回拨、连接创建生命周期...
  14. 数据结构 - 线性表顺序存储结构
  15. pixhawk编译环境搭建--2018.4.25
  16. DCD、DTR、DSR、RTS及CTS等五个状态指示分别代表什么意思?
  17. 设计模式初探-观察者模式(OBSERVER)又称发布-订阅(Publish-Subscribe)依赖(Dependents)
  18. nps p2p穿透中遇到的NAT类型过低的问题解决
  19. 计算机的传播速度和传播速率一样吗,U盘传输速度和什么有关?
  20. 8月12日 JPA复杂查询

热门文章

  1. 容器和泛型 容器重点掌握
  2. k touch for android,k touch for android
  3. html php交互json 结果集,基于HTML模板和JSON数据的JavaScript交互(移动端)
  4. java call.invoke_java invoke 以及 webservice详解,求助
  5. axure文本框添加水印_Axure教程:限制输入框输入字数
  6. STORM启动与部署TOPOLOGY
  7. 计算机培训实践反思模板,基于问题解决式课堂教学模式的反思
  8. JavaWeb9大内置对象的作用与作用域
  9. 人体识别_你知道人脸识别,但你知道人体识别吗?ReID技术了解一下!
  10. Mac下使用Docker快速搭建pinpont追踪系统APM