HDU 1058 Humble Numbers (动规+寻找丑数问题)
Humble Numbers
Total Submission(s): 16742 Accepted Submission(s): 7280
Write a program to find and print the nth element in this sequence
1
2
3
4
11
12
13
21
22
23
100
1000
5842
0
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.
pid=1024" target="_blank">1024
1025 1081大神找规律,我这样的弱菜仅仅能看别人代码了。。orz。
分别乘2,乘3,乘5。乘7。哪个小取哪个,乘过之后,p2,p3,p5。p7的值还要改变一下。。。
事实上就是一个光搜的过程,,,。题目。
每一个数都能够分解成有限个2 3 5 7 的乘积,dp方程为dp[i]=f[i]=min(f[a]*2,min(f[b]*3,min(f[c]*5,f[d]*7)))
找到比f[i-1]大且最小的数。在这里用到了滚动查找;
以下详解:
a表示f[]数组中,下标为a的数*2 可能得到当前的 f[i];若是则++
b表示f[]数组中,下标为b的数* 3 可能得到当前的f[i];若是则++
c表示f[]数组中,下标为b的数* 5 可能得到当前的f[i];若是则++
d表示f[]数组中,下标为b的数* 7 可能得到当前的f[i];若是则++
求出他们中的min,则为f[i];
假设一个数的质因子仅仅有2、3、5或7。那么这个数被称为Humble Numbers(差数)。 将正整数正序排列(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 12, 14, 15, 16, 18, 20, 21, 24, 25, 27, ... )你会发现前20个Humble Numbers(差数)。
如今,你的任务是编写一个程序将第n个Humble Numbers(差数)找出并打印出来。
输入规范:
输入的数有很多组。当中的n均满足1 <= n <= 5842。假设输入0,则表示终止。
输出规范:
对于每一组数。分列几行输出“The nth humble number is number.”,当中nth必须依据英语词法规范写为“1st、2nd、3rd、4th、5th、6th、7th、8th、9th、10th... ”。
还用到英语。
。。orz。
一般来说个位数是1为st,个位数为2是nd。个位数为3是rd;可是有例外。11。 12 ,13均为th,同一时候111,112,113均为th。除此之外,其它都是th;
代码:
#include <iostream>
#include <algorithm>
using namespace std;
#define M 50000
__int64 min(__int64 a,__int64 b,__int64 c,__int64 d){
__int64 x=a<b?a:b;
__int64 y=c<d?c:d;
return x<y?x:y;
}
__int64 vis[M];
int main(__int64 p2,__int64 p3,__int64 p5,__int64 p7)
{
int i=1,n,cur;
p2=p3=p5=p7=1;
memset(vis,0,sizeof(vis)); vis[1]=1;
while(vis[i]<=2000000000)
{
vis[++i]=min(vis[p2]*2,vis[p3]*3,vis[p5]*5,vis[p7]*7);
if(vis[i]==vis[p2]*2) p2++;
if(vis[i]==vis[p3]*3) p3++;
if(vis[i]==vis[p5]*5) p5++;
if(vis[i]==vis[p7]*7) p7++;
}
while(scanf("%d",&n)!=EOF,n)
{
string ss;
if(n%10==1&&n%100!=11)
printf("The %dst humble number is %I64d.\n",n,vis[n]);
else if(n%10==2&&n%100!=12)
printf("The %dnd humble number is %I64d.\n",n,vis[n]);
else if(n%10==3&&n%100!=13)
printf("The %drd humble number is %I64d.\n",n,vis[n]);
else
printf("The %dth humble number is %I64d.\n",n,vis[n]); }
return 0;
}
HDU 1058 Humble Numbers (动规+寻找丑数问题)的更多相关文章
- 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】
题意:给出丑数的定义,只含有2,3,5,7这四个素数因子的数称为素数.求第n个丑数. 可以先观察几个丑数得出规律 1:dp[1] 2:min(1*2,1*3,1*5,1*7) 3:min(2*2,1* ...
- 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(离线打表)
题目链接: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(构造?枚举?)
题意: 一个数的质因子如果只是2,3,5,7中的若干个.则这个数叫做humble number. 例如:1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 12, 14, 15, 16, 1 ...
- 九度OJ 1214 寻找丑数【算法】
题目地址:http://ac.jobdu.com/problem.php?pid=1214 题目描述: 把只包含因子2.3和5的数称作丑数(Ugly Number).例如6.8都是丑数,但14不是,因 ...
- 基于visual Studio2013解决面试题之0701寻找丑数
题目
随机推荐
- HTML5学习笔记 Geolocation(地理定位)
HTML5 Geolocation(地理定位)用于定位用户的位置. 定位用户的位置 html5 Geolocation API用于获得用户的地理位置 鉴于该特性可能低侵犯用户的隐私,除非用户同意,否则 ...
- C#串口通信程序实现无感知签到与答题
最近公司项目上线,之前利用串口通讯实现校牌的无感知签到程序, 项目上线以后刚刚好有时间把之前的出现的问题做下记录,废话不多,直接到主题 串口介绍: 串行接口简称串口,也称串行通信接口或串行通讯接口(通 ...
- 用Visual studio2012在Windows8上开发内核驱动监视进程创建
在Windows NT中,80386保护模式的“保护”比Windows 95中更坚固,这个“镀金的笼子”更加结实,更加难以打破.在Windows 95中,至少应用程序I/O操作是不受限制的,而在Win ...
- 如何在C++中调用C程序
注意这里的C调用C++或者C++调用C意思是.c文件中调用.cpp文件中代码,或者相反. C++和C是两种完全不同的编译链接处理方式,如果直接在C++里面调用C函数,会找不到函数体,报链接错误. 要解 ...
- freeswitch 把SIP注册信息数据库从SQLITE 改为MYSQL的方法
实际线上应用中,在线注册人数超过4000 ,SQLITE就吃不消了,容易造成锁表,考虑转入MYSQL,查了下官网 超过转入了MYSQL. https://wiki.freeswitch.org/wik ...
- IOS7 隐藏状态栏 (电池栏)
电池状态栏. //方法一(代码设置): 现在ios7已经更改为透明,并且不占用屏幕高度.其中隐藏及显示的方法如下: 在uiviewcontroller的子类下,调用: if ([self re ...
- vue2 vue-router2 webpack1
转自:http://www.qinshenxue.com/article/20161106163608.html: 项目地址:https://github.com/qinshenxue/vue2-vu ...
- Python异常处理try...except...finally raise assert
异常处理:try ...except try代码块放置容易发生异常的语句:except代码块放置处理异常的语句try ...except...finally finally代码快是任何时候都会执行的 ...
- eclipse生成export生成jar详解
使用eclipse打jar包可能还有很多人不是很了解,今天特意测试整理一番. 打jar包有3种形式 JAR file JAR Javadoc ja ...
- 一款基于jQuery的热点新闻Tab选项卡插件
今天要分享的jQuery焦点图插件非常适合展示热点新闻,之前我们分享过好多基于jQuery的焦点图插件,效果都还很不错.它可以在图片上方展示文字标题,并且焦点图的切换按钮时tab风格的,图片切换也十分 ...