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* ...
随机推荐
- nginx+keepalive主从双机热备+自动切换解决方案
环境采集cenots 6.3 64位迷你安装,因为安装前,你需要做一些工作 yum install -y make wget 如果你愿意可以更新下系统,更换下yum源. 1.安装keepalive 官 ...
- asp.net总结(一)
前言 asp.net的视频不是很多,但是中间由于毕业论文等一些事情.花的时间比较长,知识所以整体上学习的也不是很连贯 打算在总结的时候来复习一下这些知识.只能是大概的来了解asp.net到底有哪些东西 ...
- Java基础知识强化98:Apache和Tomcat服务器的联系与区别
1. Apache 和 Tomcat 都是web网络服务器,两者既有联系又有区别,在进行HTML.PHP.JSP.Perl等开发过程中,需要准确掌握其各自特点,选择最佳的服务器配置. Apache是w ...
- Javascript 第一阶段 学习使用总结
JavaScript 是一种轻量级的编程语言.JavaScript 是可插入 HTML 页面的编程代码.脚本可被放置在 HTML 页面的 <body> 和 <head> 部分中 ...
- Wijmo 日历插件
说明: 因为项目(OA)的需要,我负责开发日程的模块,相信大家用过谷歌的日历了吧,是不是觉得挺好用,但又苦于无法实现? 这里告诉你一个很好的插件,Wijmo,而里面有一个类似谷歌的日历事件插件,真的很 ...
- Android-Eclipse汉化
首先下载好汉化包 解压后得到eclipse文件夹 然后在eclipse根目录下创建language文件夹 将eclipse文件夹放入 在eclipse根目录下创建links文件夹 然后在里面添加这个文 ...
- java的练习
import java.awt.GridLayout; import javax.swing.ImageIcon; import javax.swing.JButton; import javax.s ...
- Jquery.Sorttable 桌面拖拽自定义
<!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8&quo ...
- android bindService()
bindService简介 Service一般用于不用显示,运行在后台的服务. startService 是我们最常用的启动Service的方法.而如何让service与其他组件通信呢?一般在一个进程 ...
- iOS 改变UITextField中光标颜色
第一种: [[UITextField appearance] setTintColor:[UIColor blackColor]]; 这个方法会影响整个app的所有UITextFiled... 第二种 ...