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.
打表做的。
题意输出的格式挺难懂的。。。
解释一下格式:
如果输入的数:
对10取余,余数等于1且对100取余,余数不为11.则输出的是”st”;
对10取余,余数等于2且对100取余,余数不为12.则输出的是”nd”;
对10取余,余数等于3且对100取余,余数不为13.则输出的是”rd”;
其他的全部为“th”。
import java.util.Scanner;
public class Main{
static int db[] = new int[5845];
public static void main(String[] args) {
dabiao();
Scanner sc = new Scanner(System.in);
while(sc.hasNext()){
int n = sc.nextInt();
if(n==0){
return;
}
System.out.printf("The %d", n);
if(n%10 == 1 && n%100 != 11){
System.out.printf("st");
}
else if(n%10 == 2 && n%100 != 12){
System.out.printf("nd");
}
else if(n%10 == 3 && n%100 != 13){
System.out.printf("rd");
}
else System.out.printf("th");
System.out.printf(" humble number is %d.",db[n]);
System.out.println();
}
}
private static void dabiao() {
db[1]=1;
int a1=1,a2=1,a3=1,a4=1;
for(int i=2;i<=5842;i++){
db[i]=min4(db[a1]*2,db[a2]*3,db[a3]*5,db[a4]*7);
if(db[i]==db[a1]*2){
a1++;
}
if(db[i]==db[a2]*3){
a2++;
}
if(db[i]==db[a3]*5){
a3++;
}
if(db[i]==db[a4]*7){
a4++;
}
//没有这个防护一样能过
// if(db[i]==db[i-1]){
// i--;
// }
}
}
private static int min4(int i, int j, int k, int l) {
return min(i,min(j,min(k,l)));
}
private static int min(int k, int l) {
if(k<l){
return k;
}else{
return l;
}
}
}
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 ...
- HDU 1058 Humble Numbers(离线打表)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1058 解题报告:输入一个n,输出第n个质因子只有2,3,5,7的数. 用了离线打表,因为n最大只有58 ...
- 【HDOJ】1058 Humble Numbers
简单题,注意打表,以及输出格式.这里使用了可变参数. #include <stdio.h> #define MAXNUM 5845 #define ANS 2000000000 int b ...
- 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* ...
随机推荐
- NetworkX学习笔记-5-NetworkX中怎样对多个网络赋属性,并根据属性排序
这是我在数据分析过程中遇到的实际问题,简单记录一下.这里以DiGraph为例,其他类型的网络(图)的处理方法是一样的. 按照这里:http://networkx.github.io/documenta ...
- vmware workstation11虚拟机破解版(附安装教程) 32/64位
http://kuai.xunlei.com/d/ru4IAALVJQBesH9U93e?p=20395 vmware workstation 11注册机是一款可以免费生成vmware11.0版本序列 ...
- Android常用的物理按键及其触发事件
Activity和View都能接收触摸和按键,如果响应事件只需要在继承类里复写事件函数即可:当一个视图(如一个按钮)被触摸时,该对象上的 onTouchEvent() 方法会被调用.不过,为了侦听这个 ...
- HTML学习笔记之二(回到顶部 与 回究竟部)
回到顶部 回究竟部 回到顶部的俩种方式 一.使用js $('html, body').animate({ scrollTop: 0 }, 'fast');//带动画 $('html,body').sc ...
- 通过 Linux 容器进行虚拟化
简单介绍 Linux 容器是一种轻量级"虚拟化"方法,用于在单个控制主机上同一时候执行多个虚拟装置(容器).还有一个可用来描写叙述 Linux 容器所执行的操作的术语是" ...
- [Angular + Webpack] ocLazyLoad compoment
$stateProvider .state('landing', { url: '/', views: { 'body@': { template: '<olr-landing></ ...
- shell 中条件判断
if 中的 -z 到 -d 的意思 2011-09-05 10:30 [ -a FILE ] 如果 FILE 存在则为真. [ -b FILE ] 如果 FILE 存在且是一个块特殊文件则为真. [ ...
- 多表关联查询(ORACLE版)
前言:这几天学习oracle,把自己对于关联查询的理解,记录下.如有错误请指正! 交叉连接: 交欢连接又称为“笛卡儿积连接”,是两个或多个表之间的无条件连接.一个表中所有的记录与其它表的所有的记录进行 ...
- 【酷Q插件制作】教大家做一个简单的签到插件
酷Q插件已经有很多了,社区分享一大堆,不过还是自己写才有乐趣,哈哈.不得不吐槽一下,酷Q竟然不更新了,出了个酷Q pro,还收费!!诶.不过这也影响不了咱写插件的心情,今天教大家写一个酷Q签到插件,虽 ...
- 复杂 Listview 显示 多个样式
三种方式 目前为止有三种方法让Listview现实多个样式 最简单最常用的,通过addHeaderView或addFooterView,但是只能在首尾添加 较麻烦但正规的方式,通过getViewTyp ...