#include<stdio.h>
#include<string.h>
#include<iostream>
#include<algorithm>
using namespace std;
int num[6000];//数组中的数都是他前面的数乘以2、3、5、7得到的。把乘积的大小判断出来,放在数组中。
int min(int a,int b,int c,int d)
{
if(a<=b&&a<=c&&a<=d)
{
return a;
}
else if(b<=a&&b<=c&&b<=d)
{
return b;
}
else if(c<=a&&c<=b&&c<=d)
{
return c;
}
else if(d<=a&&d<=b&&d<=c)
{
return d;
}
}
int main()
{
num[0]=0;
num[1]=1;
int two=1,three=1,five=1,sev=1;
for(int i=2;i<=5842;i++)
{
int f=min(2*num[two],3*num[three],5*num[five],7*num[sev]); //找出最小的,放在数组中。
num[i]=f;
if(num[i]==2*num[two]) ++two;//如果2*3==3*2==6了,并且6已经存在数组了,那么 让2、3分别乘数组的下一个数
if(num[i]==3*num[three]) ++three;
if(num[i]==5*num[five]) ++five;
if(num[i]==7*num[sev]) ++sev;
}
int n;
while(scanf("%d",&n)!=EOF)
{
if(n==0) break;
else if(n%100==11||n%100==12||n%100==13)//小坑:11,12,13,111,112,113.。。等,使用th的,
{
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;
}
#include<string.h>
#include<iostream>
#include<algorithm>
using namespace std;
int num[6000];//数组中的数都是他前面的数乘以2、3、5、7得到的。把乘积的大小判断出来,放在数组中。
int min(int a,int b,int c,int d)
{
if(a<=b&&a<=c&&a<=d)
{
return a;
}
else if(b<=a&&b<=c&&b<=d)
{
return b;
}
else if(c<=a&&c<=b&&c<=d)
{
return c;
}
else if(d<=a&&d<=b&&d<=c)
{
return d;
}
}
int main()
{
num[0]=0;
num[1]=1;
int two=1,three=1,five=1,sev=1;
for(int i=2;i<=5842;i++)
{
int f=min(2*num[two],3*num[three],5*num[five],7*num[sev]); //找出最小的,放在数组中。
num[i]=f;
if(num[i]==2*num[two]) ++two;//如果2*3==3*2==6了,并且6已经存在数组了,那么 让2、3分别乘数组的下一个数
if(num[i]==3*num[three]) ++three;
if(num[i]==5*num[five]) ++five;
if(num[i]==7*num[sev]) ++sev;
}
int n;
while(scanf("%d",&n)!=EOF)
{
if(n==0) break;
else if(n%100==11||n%100==12||n%100==13)//小坑:11,12,13,111,112,113.。。等,使用th的,
{
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;
}
本文介绍了一种算法,用于查找序列中的特定位置的数,该序列由2、3、5、7相乘得到。文章详细解释了如何通过比较和最小化计算结果来优化搜索过程,特别关注于处理特殊整数情况,如11、12、13等。通过实例和代码实现,读者可以深入了解序列生成和高效搜索技术。
947

被折叠的 条评论
为什么被折叠?



