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* ...
随机推荐
- 把玩Alpine linux(二):APK包管理器
导读 Alpine Linux非常精简,开机内存占用也在二三十兆大,没有拆箱即用,就需要我们自己去做一些了解和配置 Alpine Linux的优劣 优势 Alpine Linux的Docker镜像特点 ...
- Python是如何进行内存管理
三个方面:一对象的引用计数机制,二垃圾回收机制,三内存池机制 一.对象的引用计数机制 Python内部使用引用计数,来保持追踪内存中的对象,所有对象都有引用计数. 引用计数增加的情况: 1,一个对象分 ...
- 如何在 Linux 中查找最大的 10 个文件
https://linux.cn/article-9495-1.html
- Java Serializable的使用和transient关键字使用小记(转载)
一:Serializable 1.持久化的简单介绍: “持久化”意味着对象的“生存时间”并不取决于程序是否正在执行——它存在或“生存”于程序的每一次调用之间.通过序列化一个对象,将其写入磁盘,以后在程 ...
- [转帖]Docker 清理占用的磁盘空间
Docker(二十七)-Docker 清理占用的磁盘空间 https://www.cnblogs.com/zhuochong/p/10076599.html docker system docker ...
- CLOUD常用表
采购采购订单(t_PUR_POOrder, t_PUR_POOrderEntry)-收料通知单(T_PUR_Receive,T_PUR_ReceiveEntry)-采购入库单(T_STK_INSTOC ...
- 解决Jupyter notebook[import tensorflow as tf]报错
参考: https://blog.csdn.net/caicai_zju/article/details/70245099
- spring-01
Spring概述 概述 Spring是一个开源框架 为企业级开发而生 是一个IOC[DI]和AOP容器框架 有许多优良特性 非侵入式:基于Spring开发的应用中的对象可以不依赖Spring的API. ...
- 莫烦theano学习自修第十天【保存神经网络及加载神经网络】
1. 为何保存神经网络 保存神经网络指的是保存神经网络的权重W及偏置b,权重W,和偏置b本身是一个列表,将这两个列表的值写到列表或者字典的数据结构中,使用pickle的数据结构将列表或者字典写入到文件 ...
- String 常见的十种方法!
public class ZiFuChuan { public static void main(String[] args) { ZiFuChuanFangFa f=new ZiFuChuanFan ...