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 题解的更多相关文章

  1. HDOJ 1058 Humble Numbers(打表过)

    Problem Description A number whose only prime factors are 2,3,5 or 7 is called a humble number. The ...

  2. HDOJ(HDU).1058 Humble Numbers (DP)

    HDOJ(HDU).1058 Humble Numbers (DP) 点我挑战题目 题意分析 水 代码总览 /* Title:HDOJ.1058 Author:pengwill Date:2017-2 ...

  3. HDU 1058 Humble Numbers (DP)

    Humble Numbers Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)To ...

  4. hdu 1058:Humble Numbers(动态规划 DP)

    Humble Numbers Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)To ...

  5. HDU 1058 Humble Numbers (动规+寻找丑数问题)

    Humble Numbers Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) T ...

  6. 【HDOJ】1058 Humble Numbers

    简单题,注意打表,以及输出格式.这里使用了可变参数. #include <stdio.h> #define MAXNUM 5845 #define ANS 2000000000 int b ...

  7. HDU 1058 Humble Numbers(离线打表)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1058 解题报告:输入一个n,输出第n个质因子只有2,3,5,7的数. 用了离线打表,因为n最大只有58 ...

  8. hdu 1058 Humble Numbers

    这题应该是用dp来做的吧,但一时不想思考了,写了个很暴力的,类似模拟打表,然后排序即可,要注意的是输出的格式,在这里wa了一发,看了别人的代码才知道哪些情况没考虑到. #include<cstd ...

  9. 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* ...

随机推荐

  1. 【学习总结】Markdown 使用的正确姿势

    参考资料:Learning-Markdown 入门参考 注:原博可能对GitHub中的Markdown格式更适用. 有时间可以再GitHub中尝试并写一篇相关攻略. ps:在王熊猫的GitHub里也有 ...

  2. ascii、unicode、utf-8、gbk 区别

    原文:https://blog.csdn.net/u010262331/article/details/46013905 ASCII:遇上0×10, 终端就换行: 遇上0×07, 终端就向人们嘟嘟叫: ...

  3. vue实现双向数据绑定之Object.defineProperty()篇

    前言 vue.js中使用ES5的Object.defineProperty()实现数据的双向绑定 Object.defineProperty()原理 Object.defineProperty()可以 ...

  4. lumen 5.6 设置APP_KEY为32位长的随机字符串

    在 App\Console\Commands下 添加以下内容的KeyGenerateCommand.php文件 <?php namespace App\Console\Commands; use ...

  5. 05 Hadoop 设置块的大小

    1.是在hdfs的配置文件中配置 2.是在app程序中设置 注意:假设配置文件的最大是   20K   最小是 10K   文件大小为72  块数就是 4 在程序中设置最大为15K    切割块数  ...

  6. 【转帖】Linux定时任务Crontab命令详解

    Linux定时任务Crontab命令详解 https://www.cnblogs.com/intval/p/5763929.html 知道有crontab 以及 at 命令 改天仔细学习一下 讲sys ...

  7. java内部类 和外部类的区别

    java 内部类和静态内部类的区别  详细连接https://www.cnblogs.com/aademeng/articles/6192954.html 下面说一说内部类(Inner Class)和 ...

  8. Linux基础学习笔记4-文本处理

    本章内容 抽取文本的工具 文件内容:less和cat 文件截取:head和tail 按列抽取:cut 按关键字抽取:grep 文件查看 文件查看命令:cat,tac,rev cat [OPTION] ...

  9. NIO和经典IO

    NIO未必更快,在Linux上使用Java6完成的测试中,多线程经典I/O设计胜出NIO30%左右 异步I/O强于经典I/O:服务器需要支持超大量的长期连接,比如10000个连接以上,不过各个客户端并 ...

  10. Django--CRM--QueryDict, 模糊搜索, 加行级锁

    一 . QueryDict的修改 # QueryDict正常是不允许修改的,要想往里面添加内容,需要另mutable=True dic = request.GET print(dic) # <Q ...