Hdoj 1058.Humble Numbers 题解
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
University of Ulm Local Contest 1996
思路
丑数的因子只有2,3,5,7,所以可以用从1开始乘这四个因子得到新的数字之后加到数组里面,更新1为2,如此迭代计算下去到算出5842个值,但是这样会存在重复元素问题还要考虑去重问题,数组的元素也不是有序的。
改善这个思路可以有:
设数组a存放所有的丑数,设立4个游标pos分别对应4个因子2,3,5,7,一个数组暂时存储所得的结果。每次都用游标值*4个因子,然后找到最小的值放入数组a,直到数量为5842为止。
注意一个坑:111的英文是one hundred and eleventh,是th结尾的!!!!!!,112,113同理,实际上不止是这三个数,只要符合一定的特征。
代码
#include<bits/stdc++.h>
using namespace std;
int num[6000];
int findMin(int a[],bool f[],int len)
{
int minvalue = a[0];
for(int i=0;i<len;i++)
minvalue = min(minvalue, a[i]);
for(int i=0;i<len;i++)
minvalue == a[i] ? f[i] = true : f[i] = false;
return minvalue;
}
void Init()
{
num[1] = 1;
bool vis[6000];
int tmp[4];
int pos[4] = {1,1,1,1};
int fac[4] = {2,3,5,7};
for(int i=2;i<=5842;i++)
{
for(int j=0;j<4;j++)
tmp[j] = num[pos[j]] * fac[j];
int minvalue = findMin(tmp,vis,4);
num[i] = minvalue;
for(int j=0;j<4;j++)
if(vis[j])
pos[j]++;
}
}
int main()
{
int n;
Init();
while(cin>>n && n!=0)
{
string suffix = "th";
if(n%10==1 && n%100!=11)
suffix = "st";
else if(n%10==2 && n%100!=12)
suffix = "nd";
else if(n%10==3 && n%100!=13)
suffix = "rd";
cout<<"The "<<n<<suffix<<" humble number is "<<num[n]<<"."<<endl;
}
return 0;
}
Hdoj 1058.Humble Numbers 题解的更多相关文章
- HDOJ 1058 Humble Numbers(打表过)
Problem Description A number whose only prime factors are 2,3,5 or 7 is called a humble number. The ...
- HDOJ(HDU).1058 Humble Numbers (DP)
HDOJ(HDU).1058 Humble Numbers (DP) 点我挑战题目 题意分析 水 代码总览 /* Title:HDOJ.1058 Author:pengwill Date:2017-2 ...
- HDU 1058 Humble Numbers (DP)
Humble Numbers Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)To ...
- hdu 1058:Humble Numbers(动态规划 DP)
Humble Numbers Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)To ...
- HDU 1058 Humble Numbers (动规+寻找丑数问题)
Humble Numbers Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) T ...
- 【HDOJ】1058 Humble Numbers
简单题,注意打表,以及输出格式.这里使用了可变参数. #include <stdio.h> #define MAXNUM 5845 #define ANS 2000000000 int b ...
- HDU 1058 Humble Numbers(离线打表)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1058 解题报告:输入一个n,输出第n个质因子只有2,3,5,7的数. 用了离线打表,因为n最大只有58 ...
- hdu 1058 Humble Numbers
这题应该是用dp来做的吧,但一时不想思考了,写了个很暴力的,类似模拟打表,然后排序即可,要注意的是输出的格式,在这里wa了一发,看了别人的代码才知道哪些情况没考虑到. #include<cstd ...
- HDU 1058 Humble Numbers【DP】
题意:给出丑数的定义,只含有2,3,5,7这四个素数因子的数称为素数.求第n个丑数. 可以先观察几个丑数得出规律 1:dp[1] 2:min(1*2,1*3,1*5,1*7) 3:min(2*2,1* ...
随机推荐
- 使用junit测试
package creeper; import java.util.Scanner; public class size { private static int intercePosition = ...
- react 组件列表
let data=[ [ '同时入选IMDB250和豆瓣电影250的电影', '带你进入不正常的世界', '用电[影]来祭奠逝去的岁月', '女孩们的故事[电影]', '', '使用 App [找电影 ...
- linux重装后配一些库
#先要设置软件源 sudo apt-get update sudo apt-get upgrade #播放器 sudo apt-get install smplayer qt sudo apt-get ...
- C# Note30: 网络爬虫
用C#实现网络爬虫(一) 用C#实现网络爬虫(二) 基于C#.NET的高端智能化网络爬虫(一)(反爬虫哥必看) 基于C#.NET的高端智能化网络爬虫(二)(攻破携程网) C#获取网页内容的三种方式
- 爬虫 之Requests库的详细使用
1.什么是Requests? Requests是用Python语言编写的,基于urllib3来改写的,采用Apache2 Licensed 来源协议的HTTP库. 它比urllib更加方便,可以节约我 ...
- django之路由层
一 Django中路由的作用 二 简单的路由配置 三 有名分组 四 路由分发 五 反向解析 六 名称空间 七 django2.0版的path 一 Django中路由的作用 URL配置(URLconf) ...
- java学习之—链表(4)
/** * 使用链表实现队列 * Create by Administrator * 2018/6/19 0019 * 下午 4:37 **/ public class Link { public l ...
- SpringMVC配置三大组件
1.组件扫描器 使用组件扫描器省去在spring容器配置每个Controller类的繁琐. 使用<context:component-scan>自动扫描标记@Controller的控制器类 ...
- NFS配置与搭建
参考: Linux下NFS服务器的搭建与配置 https://www.cnblogs.com/liuyisai/p/5992511.html http://blog.51cto.com/hongten ...
- 谈谈对C#中反射的一些理解和认识(下)
在上一篇中我们列举了一些反射的常规的使用,这一篇我们将介绍一些关于关于反射的高级属性,这些包括创建对反射的性能的总结以及如何优化反射性能,以及通过InvokeMember的方法如何去调用反射等等,通过 ...