题目链接:

http://acm.hdu.edu.cn/showproblem.php?pid=1058

Humble Numbers

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 29253    Accepted Submission(s): 12809

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.
题意:
如果一个数的因子只有2,3,5,7那么这个数叫差数,此题就是求第n个差数是多少?
分析:
求第n个a[n],则肯定是前面的第n-1个数中的某一个与2,3,5,7中的一个的乘积,取最小的哪一个,取的哪一个对应的指针就向前移动一位
 
#include<bits/stdc++.h>
#define max_v 5850
int i2=1,i3=1,i5=1,i7=1,i;
int a[max_v];
using namespace std;
int f()
{
int x=min ( min ( 2*a[i2], 3*a[i3] ),
min ( 5*a[i5], 7*a[i7] ) );
if(x==2*a[i2])
{
++i2;
}else if(x==3*a[i3])
{
++i3;
}else if(x==5*a[i5])
{
++i5;
}else if(x==7*a[i7])
{
++i7;
}
return x;
}
int main()
{
a[0]=0;
a[1]=1;
for(i=2;i<max_v;i++)
{
a[i]=f();
if(a[i]==a[i-1])
{
i--;
}
}
int n;
while(~scanf("%d",&n))
{
if(n==0)
break;
printf("The %d",n);
if(n%100==11||n%100==12||n%100==13)
{
printf("th ");
}else if(n%10==1)
{
printf("st ");
}else if(n%10==2)
{
printf("nd ");
}else if(n%10==3)
{
printf("rd ");
}else
{
printf("th ");
}
printf("humble number is %d.\n",a[n]);
}
return 0;
}

  

HDU 1058(打表)的更多相关文章

  1. hdu 1058 dp.Humble Numbers

    Humble Numbers Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u Subm ...

  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(离线打表)

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

  4. hdu 4542 打表+含k个约数最小数

    http://acm.hdu.edu.cn/showproblem.php?pid=4542 给出一个数K和两个操作 如果操作是0,就求出一个最小的正整数X,满足X的约数个数为K. 如果操作是1,就求 ...

  5. HDU 1223 打表 + 大数

    http://acm.hdu.edu.cn/showproblem.php?pid=1223 一般遇到这些题,我都是暴力输出前几项,找规律.未果. 然后输出n = 1时候,以A开始,有多少个答案, n ...

  6. hdu 1058 Humble Numbers

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

  7. hdu 3652 打表

    思路:直接打表 #include<cstdio> #include<vector> #include<cmath> #include<iostream> ...

  8. hdu 5183 hash表

    BC # 32 1002 题意:给出一个数组 a 和一个数 K ,问是否存在数对( i , j ),使 a i   - a i + 1 +……+ (-1)j - i  a j : 对于这道题,一开始就 ...

  9. hdu 4715(打表)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4715 思路:先打个素数表,然后判断一下就可以了. #include<iostream> # ...

随机推荐

  1. C# 调用C++DLL 类型转换

    内容转自网上····这里做 备份··· 原文链接: http://blog.csdn.net/miss_easy/article/details/52470964 /C++中的DLL函数原型为 //e ...

  2. MySQL:PyMySQL&ORM

    一.PyMySQL介绍 PyMySQL 是在 Python3.x 版本中用于连接 MySQL 服务器的一个库,Python2中则使用mysqldb. Django中也可以使用PyMySQL连接MySQ ...

  3. sql:PostgreSQL

    PostgreSQL sql script: -- Database: geovindu -- DROP DATABASE geovindu; CREATE DATABASE geovindu WIT ...

  4. PhpStorm 自定义快捷键

    PhpStorm 两个重要快捷键 1.CTRL+SHIFT+A 用于恢复隐藏项 2.文件之间的快速跳转 CTRL+SHIFT+N 3.自定义快捷键 第一步:打开左上角file-setting 第二步: ...

  5. 手写堆优化dijkstra

    \(dijkstra\) 算法的堆优化,时间复杂度为\(O(n+m)\log n\) 添加数组\(id[]\)记录某节点在堆中的位置,可以避免重复入堆从而减小常数 而这一方法需要依托手写堆 #incl ...

  6. RHEL5.X 重启网卡出现./network-functions: line 78: .: ifcfg-eth0: file not found

    错误信息: 红帽RHEL5.5系统,重启网卡报错 [root@localhost network-scripts]# service network restart Shutting down int ...

  7. MySQL数据库(10)----IN 和 NOT IN 子查询

    当子查询要返回多个行来与外层查询进行比较运算时,可以使用运算符 IN 和 NOT IN.它们会测试某个给定的比较值是否存在于某一组值里.如果外层查询里的行与子查询返回的某一个行相匹配,那么 IN 的结 ...

  8. Non-resolvable parent POM for com.*******

    场景: 同事新打了一个jar包到私服里面,自己删除了本地对应的中央仓库的依赖包,再次重新下载.  于是我又打开了一个idea的窗口重新引入这个项目,然后重新下载依赖的服务. 结果就一直报这个问题... ...

  9. RoCE、softRoCE与iWRAP

    RoCE - RDMA over Converged Ethernet 以太网在全球互联的广域网中毫无异议的老大,但在高带宽.低延时的专有网络领域却明显混不开.伴随网络融合概念兴起,IETF发布了DC ...

  10. java将int类型的变量转化成String类型的

    第一种方法:String的valueOf方法,int i=5;String s=String.valueOf(i);第二种方法,直接在int后面加一个空的字符串,因为在java里面,默认任务int类型 ...