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* ...
随机推荐
- git的用法步骤讲解
1.创建全局的本地用户名 git config --global user.name "teamemory" git config --global user.email &quo ...
- Day6 Pyhton基础之文件操作(五)
能调用方法的一定是对象 1.对文件操作流程 打开文件,得到文件句柄并赋值给一个变量 通过句柄对文件进行操作 关闭文件 #-*-codeing-*-:UTF-8 #author:Weina Pang # ...
- 为github添加ssh key
用git关联github上的远程仓库前需要先为github添加ssh key 一.检查本机是否生成ssh key 本地查找.ssh文件,其中id_rsa.pub中的内容就是ssh key 二.为git ...
- CentOS搭建OpenVPN以及WIN&Android&iOS的安装连接
OpenVPNhttp://info.swufe.edu.cn/vpn/openvpn/#2 苹果.安卓智能手机openvpn的设置_百度经验https://jingyan.baidu.com/art ...
- linux之常见错误
在日常开发中,尤其是在Linux中进行操作的时候,经常会碰到各种各样的错误.记录一下,熟能生巧,慢慢参透linux的奥秘 1) 在安装ssl证书的时候,发生certbot命令无法使用的情况 解决方案: ...
- Docker操作删除所有容器镜像
借鉴博客:https://www.cnblogs.com/yanyouqiang/p/8301856.html https://blog.csdn.net/wy_97/article/details/ ...
- Day3-2 函数之递归
递归 定义:一个函数在 内部调用自己,就称为递归. # 如何让10不停的除以2,直到不能除为止. n = 10 while True: n = int(n /2) print(n) if n == 0 ...
- C# Note29: Close()和Dispose()的区别
待更! 深入解析Close()和Dispose()的区别
- C# Note1:深入浅出WPF-MVVM篇
一.资源说明 (1)配套视频:深入浅出WPF 讲的不错! 待更!
- clone内容包含select2
如果克隆的内容包含select2,克隆之后要先删除select之后自动生成的span,再对select2进行初始化,生成新的元素.