首页 技术 正文
技术 2022年11月15日
0 收藏 896 点赞 3,852 浏览 2839 个字

Humble Numbers

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)

Total Submission(s): 16742    Accepted Submission(s): 7280
Problem DescriptionA 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
 InputThe 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.
 OutputFor 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.

 SourceUniversity of Ulm Local Contest 1996 RecommendJGShining   |   We have carefully selected several similar problems for you:  1176 1421 1025 1081

大神找规律,我这样的弱菜仅仅能看别人代码了。。orz。

分别乘2,乘3,乘5。乘7。哪个小取哪个,乘过之后,p2,p3,p5。p7的值还要改变一下。。。

事实上就是一个光搜的过程,,,。题目。

每一个数都能够分解成有限个2 3 5 7 的乘积,dp方程为dp[i]=f[i]=min(f[a]*2,min(f[b]*3,min(f[c]*5,f[d]*7)))

找到比f[i-1]大且最小的数。在这里用到了滚动查找;

以下详解:

a表示f[]数组中,下标为a的数*2 可能得到当前的 f[i];若是则++

b表示f[]数组中,下标为b的数* 3 可能得到当前的f[i];若是则++

c表示f[]数组中,下标为b的数* 5 可能得到当前的f[i];若是则++

d表示f[]数组中,下标为b的数* 7 可能得到当前的f[i];若是则++

求出他们中的min,则为f[i];

 假设一个数的质因子仅仅有2、3、5或7。那么这个数被称为Humble Numbers(差数)。将正整数正序排列(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 12, 14, 15, 16, 18, 20, 21, 24, 25, 27, ... )你会发现前20个Humble Numbers(差数)。
    如今,你的任务是编写一个程序将第n个Humble Numbers(差数)找出并打印出来。
    
输入规范:
输入的数有很多组。当中的n均满足1 <= n <= 5842。假设输入0,则表示终止。
输出规范:
对于每一组数。分列几行输出“The nth humble number is number.”,当中nth必须依据英语词法规范写为“1st、2nd、3rd、4th、5th、6th、7th、8th、9th、10th... ”。

还用到英语。

。。orz。

一般来说个位数是1为st,个位数为2是nd。个位数为3是rd;可是有例外。11。 12 ,13均为th,同一时候111,112,113均为th。除此之外,其它都是th;

代码:

#include <iostream>
#include <algorithm>
using namespace std;
#define M 50000
__int64 min(__int64 a,__int64 b,__int64 c,__int64 d){
__int64
x=a<b?a:b;
__int64
y=c<d?c:d;
return
x<y?x:y;
}
__int64
vis[M];
int main(__int64
p2,__int64 p3,__int64 p5,__int64 p7)
{
int
i=1,n,cur;
p2=p3=p5=p7=1;
memset(vis,0,sizeof(vis)); vis[1]=1;
while(
vis[i]<=2000000000)
{

vis[++i]=min(vis[p2]*2,vis[p3]*3,vis[p5]*5,vis[p7]*7);
if(
vis[i]==vis[p2]*2) p2++;
if(
vis[i]==vis[p3]*3) p3++;
if(
vis[i]==vis[p5]*5) p5++;
if(
vis[i]==vis[p7]*7) p7++;
}
while(
scanf("%d",&n)!=EOF,n)
{

string ss;
if(
n%10==1&&n%100!=11)
printf("The %dst humble number is %I64d.\n",n,vis[n]);
else if(
n%10==2&&n%100!=12)
printf("The %dnd humble number is %I64d.\n",n,vis[n]);
else if(
n%10==3&&n%100!=13)
printf("The %drd humble number is %I64d.\n",n,vis[n]);
else

printf("The %dth humble number is %I64d.\n",n,vis[n]); }
return
0;
}
相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:8,996
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,510
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,353
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,137
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,770
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,848