丑数

xiaoxiao2021-02-28  32

如果一个数的所有质因子全部由2, 3, 5, 7中的任意多个组成, 那我们把这个数称为H数. 比如: 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 12, 14, 15, 16, 18, 20, 21, 24, 25, 27 是前 20 个H数  指出第n个H数是多少.  Input多组输入. 每组输入包含一个正整数 n (1 <= n <= 5842).  n = 0 时结束输入.  Output对于每组输入, 输出 "The nth humble number is number.".  注意 "nth"的词尾应根据整数 n 的序数词词尾而改变.  Sample Input 1 2 3 4 11 12 13 21 22 23 100 1000 5842 0Sample 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.

每个数都由2,3,5,7组成,所以新的数由之前的数生成。

/* 打表 ,速度更快 */ #include<stdio.h> #include<algorithm> using namespace std; int main() { int n1=1; long long a[6000]={0}; int m1,m2,m3,m4; m1=m2=m3=m4=1; a[1]=1; n1++; while(n1<=5842) { int min1=min(a[m1]*2,a[m2]*3); int min2=min(a[m3]*5,a[m4]*7); a[n1]=min(min1,min2); if(a[n1]==a[m1]*2)m1++; if(a[n1]==a[m2]*3)m2++; if(a[n1]==a[m3]*5)m3++; if(a[n1]==a[m4]*7)m4++; n1++; } int n; while(scanf("%d",&n)==1) { if(n==0)break; int tem=n; int t=n+1; if(tem==1&&n0!=11) printf("The %dst humble number is %lld.\n",n,a[t-1]); if(tem==2&&n0!=12) printf("The %dnd humble number is %lld.\n",n,a[t-1]); if(tem==3&&n0!=13) printf("The %drd humble number is %lld.\n",n,a[t-1]); if(tem!=1&&tem!=2&&tem!=3||n0>10&&n0<14) printf("The %dth humble number is %lld.\n",n,a[t-1]); } }
转载请注明原文地址: https://www.6miu.com/read-2650259.html

最新回复(0)