HDU 1058 Humble Numbers (DP)
Humble Numbers
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 18238 Accepted Submission(s): 7934
Write a program to find and print the nth element in this sequence
2
3
4
11
12
13
21
22
23
100
1000
5842
0
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.
说DP有点牵强,不过也算是用前面的状态推出后面的状态,英语知识是个易错点。
思路:2,5,7,9是丑数,丑数的2,5,7,9倍也是丑数,所以从1开始,对每个数计算计算它的2,5,7,9倍,算出来的结果又再翻倍。难免会遇到这种考察其它乱七八糟的知识点的题,输出时的后缀很容易错,在英语里如果一个数的个位是1,那后面跟st,2跟nd,3跟rd,其它跟th,但是注意,后面两位是11,12,13的除外,这些数后面都跟th,比如12,10013,1111,我WA了三次,算是英语差的后果吧。
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#define MAX 5845 int my_min(int a,int b,int c,int d);
int main(void)
{
int n;
int s[MAX] = {};
int i,i_2,i_3,i_5,i_7;
int box,box_2,box_3,box_5,box_7;
char cha[];
i = i_2 = i_3 = i_5 = i_7 = ; while(i < MAX - )
{
box_2 = s[i_2] * ;
box_3 = s[i_3] * ;
box_5 = s[i_5] * ;
box_7 = s[i_7] * ; box = my_min(box_2,box_3,box_5,box_7); //每次把最小的那个插入到数组里,就不用排序了
if(box == box_2) //要一直用if,因为结果可能相同
i_2 ++;
if(box == box_3)
i_3 ++;
if(box == box_5)
i_5 ++;
if(box == box_7)
i_7 ++; i ++;
s[i] = box;
} while(scanf("%d",&n) && n)
{
box = n % ;
if(box == || box == || box == )
{
strcpy(cha,"th");
goto label;
} box = n;
while(box / )
box %= ;
switch(box % )
{
case :strcpy(cha,"st");break;
case :strcpy(cha,"nd");break;
case :strcpy(cha,"rd");break;
default :strcpy(cha,"th");break;
} label:
printf("The %d%s humble number is %d.\n",n,cha,s[n - ]);
} return ;
} int my_min(int a,int b,int c,int d)
{
int min = a; min = min < b ? min : b;
min = min < c ? min : c;
min = min < d ? min : d; return min;
}
HDU 1058 Humble Numbers (DP)的更多相关文章
- 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 (动规+寻找丑数问题)
Humble Numbers Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) T ...
- 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(离线打表)
题目链接: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 ...
- [poj2247] Humble Numbers (DP水题)
DP 水题 Description A number whose only prime factors are 2,3,5 or 7 is called a humble number. The se ...
- HDU 1058 Humble Number
Humble Number Problem Description A number whose only prime factors are 2,3,5 or 7 is called a humbl ...
随机推荐
- Handler笔记
5.Handler内存泄露 在一个Activity里面,如果没有静态地创建一个Handler,有可能导致内存泄露. 因为Hander和Looper绑定,如果looper的MessageQueue有消息 ...
- Spring SimpleJdbcTemplate Querying examples
Here are few examples to show how to use SimpleJdbcTemplate query() methods to query or extract data ...
- jquery easyui添加图标扩展
easyui中有很多通过iconCls="icon-reload"这样的属性引入小图标显示,当然我们也可以自己添加自己的小图标. 方式:1.我们可以在jquery easyui的文 ...
- C#中的表达式树简介
表达式树是.NET 3.5之后引入的,它是一个强大灵活的工具(比如用在LINQ中构造动态查询). 先来看看Expression类的API接口: using System.Collections.Obj ...
- Android流量监控 思路,想法
1,开启一个服务,每5分钟跑动一次更新流量,用于能够准确记录流量 每一个小时,更新一次流量,用于清除非本月的流量 2,保存流量的时候,进行判断 a,若是数据库中保存的 ...
- Java学习笔记(五):异常处理
异常指不期而至的各种状况,如:文件找不到.网络连接失败.非法参数等.异常是一个事件,它发生在程序运行期间,干扰了正常的指令流程.而在我们的程序中需要对这些异常进行捕获和处理来避免程序卡死等情况. Ja ...
- [Windows驱动开发](二)基础知识——数据结构
本节主要介绍驱动开发的一些基础知识. 1. 驱动程序的基本组成 1.1. 最经常见到的数据结构 a. DRIVER_OBJECT驱动对象 // WDK中对驱动对象的定义 // 每个驱动程序都会有一个唯 ...
- st_Alarm_GenAlarmDealTime
USE [ChiefmesNew]GO/****** Object: StoredProcedure [dbo].[st_Alarm_GenAlarmDealTime] Script Date: 04 ...
- CSS 文本格式
整理自:(http://www.w3school.com.cn/css/css_text.asp) Text Color 颜色属性被用来设置文字的颜色. 颜色是通过CSS最经常的指定: 十六进制值 - ...
- 通过yum安装Nagios
通过yum安装Nagios 2012年04月05日 ⁄ Nagios ⁄ 暂无评论 QQ空间新浪微博腾讯微博人人网更多3 前提先自行安装好Apache+php 测试环境主监控机:CentOS ...