1058 Humble Numbers

题目详情:

 ​

Humble Numbers

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

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.
 
Source
 
Recommend
JGShining   |   We have carefully selected several similar problems for you:  1003 1159 1069 1087 1176 

 题目大意:

一个数它的因子只有2,3,5,7其中的一个或多个的任意组合的数叫丑数,现在请你写一个程序找出第N个丑数!

解题思路:

设h2=1,h3=1,h5=1,h7=1,状态转移方程:num[i]=min(min(2*num[h2],3*num[h3]),min(5*num[h5],7*num[h7])),同时用num[i]和之前的这四个数作比较,相等则使对应的h加一。

AC代码:

#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;
int main(){
    int n;
    vector<int> num(5846,0);
    num[1]=1;
    int h2=1,h3=1,h5=1,h7=1;
    for(int i=2;i<=5843;++i){
            num[i]=min(min(2*num[h2],3*num[h3]),min(5*num[h5],7*num[h7]));
            if(num[i]==2*num[h2]) h2++;
            if(num[i]==3*num[h3]) h3++;
            if(num[i]==5*num[h5]) h5++;
            if(num[i]==7*num[h7]) h7++;
    }
    while(cin>>n&&n){
        if(n%100==11||n%100==12||n%100==13) printf("The %dth humble number is %d.\n",n,num[n]); 
        else { 
            if(n%10==1) printf("The %dst humble number is %d.\n",n,num[n]); 
            else if(n%10==2)printf("The %dnd humble number is %d.\n",n,num[n]); 
            else if(n%10==3)printf("The %drd humble number is %d.\n",n,num[n]); 
            else printf("The %dth humble number is %d.\n",n,num[n]); 
        } 
    }   
    return 0;
}

推荐几款学习编程的网站

免费在线开发平台(LTPP在线开发平台 | LTPP宇宙文档

        探索编程世界的新天地,为学生和开发者精心打造的编程平台,现已盛大开启!这个平台汇集了近4000道精心设计的编程题目,覆盖了C、C++、JavaScript、TypeScript、Go、Rust、PHP、Java、Ruby、Python3以及C#等众多编程语言,为您的编程学习之旅提供了一个全面而丰富的实践环境。

        在这里,您不仅可以查看自己的代码记录,还能轻松地在云端保存和运行代码,让编程变得更加便捷。平台还提供了私聊和群聊功能,让您可以与同行们无障碍交流,分享文件,共同进步。不仅如此,您还可以通过阅读文章、参与问答板块和在线商店,进一步拓展您的知识边界。

        为了提升您的编程技能,平台还设有每日一题、精选题单以及激动人心的编程竞赛,这些都是备考编程考试的绝佳资源。更令人兴奋的是,您还可以自定义系统UI,选择视频或图片作为背景,打造一个完全个性化的编码环境,让您的编程之旅既有趣又充满挑战。

免费公益服务器(LTPP公益LINUX服务器分享 | LTPP宇宙文档

        作为开发者或学生,您是否为搭建和维护编程环境而困扰?现在,有一款免费的公共服务器,内置多种编程语言的编程环境,并且配备了在线版VS Code。让您可以随时随地在线写代码,无需复杂配置,专注于开发和学习。(PS:毕竟是免费公共的服务器,任何人都能够使用,为了数据隐私和安全,请勿上传重要数据,仅用于学习)

免费公益MYSQL(LTPP公益MYSQL分享 | LTPP宇宙文档

        作为一名开发者或学生,您是否常常为数据库环境的搭建而烦恼?是否因为预算有限而无法使用高性能的数据库服务?现在,有一款免费的MySQL服务器,专为开发者和学生量身打造,让你轻松无忧地进行开发和学习!内置在线phpmyadmin管理面板,方便用户查看数据。(PS:毕竟是免费公共的MYSQL,任何人都能够使用,为了数据隐私和安全,请勿上传重要数据,仅用于学习)

免费在线WEB代码编辑器(LTPP在线WEB编辑器 | LTPP宇宙文档

        无论你是开发者还是学生,编程环境的搭建和管理可能会占用你宝贵的时间和精力。现在,有一款强大的免费在线代码编辑器,支持多种编程语言,让您可以随时随地编写、调试和运行代码,提升编程效率,专注于创意和开发。

免费二维码生成器(LTPP二维码生成器 | LTPP宇宙文档

        无论是企业宣传、活动推广,还是个人信息分享,二维码都是一种快速、高效的信息传递方式。现在,有一款功能强大的二维码生成器,不仅易于使用,还具备多种便捷功能,帮助您更轻松地生成和管理二维码。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

WA-自动机

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值