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 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;
        }
    }
}
目录
相关文章
|
安全
电机控制中对地的处理
1.当电路中有电机时,尤其是电压比较高的电机,一定要将控制电和动力电的地隔离开。如果不隔离,电机一启动,控制电路就可能不正常,比如复位、通信不正常等。 2.如果是有单独的电机驱动,那么主控板和驱动器的地最好也分开,实在不行也可以共地。
276 0
电机控制中对地的处理
|
10月前
|
Windows
Multisim 14简易三人抢答器电路设计
Multisim 14简易三人抢答器电路设计
359 1
|
10月前
|
传感器 安全 芯片
一款双极锁存型霍尔位置传感器
一、产品特点 双极锁存型霍尔效应传感器 宽的工作电压范围: 3.8V~30V 集电极开路输出 最大输出灌电流:50mA 电源反极性保护 工作温度:-40℃~+125℃ 封装形式: SOT23-3 TX412是一款集成霍尔效应传感器,主要应用于直流无刷电机的电子信号交换。其内部包含感应磁场的霍尔电压发生器、霍尔信号放大器、提供滞回作用和清除噪声的施密特电路以及集电极开路输出。内置的电压稳压器为内部电路提供具有温度补偿的偏置电压,使其具有宽的工作电源输入范围。 北极(N)足够的磁场强度垂直作用于芯片表面,将使输出端输出低电平,而南极(S)足够的磁场强度将使输出端输出高电平。即当B
|
数据安全/隐私保护
BFS:密码锁
BFS:密码锁
基于51单片机的8八路抢答器设计
(1)主持人进行复位,依次显示8位选手的分数,8位选手分数显示结束后主持人方可按下开始按键; (2)主持人按下抢答开始按键,抢答者才可以开始抢答,数码管抢答倒计时10S; (3)抢答者按下按键,数码管显示抢答者的编号和答题剩余时间倒计时20S; (4)在20S内,抢答者答题正确,主持人按下加分按键,分数加一,答错主持人按下减分按键,分数减一(默认抢答者的初始分数为60)。
240 0
钞票(打表枚举)
钞票(打表枚举)
53 0
(二维数组打表)F. 342 and Xiangqi
(二维数组打表)F. 342 and Xiangqi
64 0