其他分享
首页 > 其他分享> > 丑数

丑数

作者:互联网

丑数

题目描述

如果一个数的素因子只包含2,3,5或7,那么我们把这种数叫做丑数。序列1,2,3,4,5,6,7,8,9,10,12,14,15,16,18,20,21,24,25,27...展示了前20个丑数。
请你编程寻找这个序列中的第n个元素。

输入

输入包含多组测试数据。每组输入为一个整数n(1<=n<=5842),当n=0时,输入结束。

输出

对于每组输入,输出一行“The nth humble number is number.”。里面的n由输入中的n值替换,“st”,“nd”,“rd”和“th”这些序数结尾的用法参照输出样例。

样例输入

1
2
3
4
11
12
13
21
22
23
100
1000
5842
0

样例输出

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.

 1 #include<iostream>
 2 using namespace std;
 3 int minnum(int a,int b,int c,int d)
 4 {
 5     if(a>b)
 6         a=b;
 7     if(a>c)
 8         a=c;
 9     if(a>d)
10         a=d;
11     return a;
12 }
13 int main()
14 {
15     int t1,t2,t3,t4,n;
16     t1=t2=t3=t4=1;
17     int a[10000];
18     a[1]=1;
19     for(int i=2;i<6000;i++)
20     {
21         a[i]=minnum(a[t1]*2,a[t2]*3,a[t3]*5,a[t4]*7);
22         if(a[i]==a[t1]*2)
23             t1++;
24         if(a[i]==a[t2]*3)
25             t2++;
26         if(a[i]==a[t3]*5)
27             t3++;
28         if(a[i]==a[t4]*7)
29             t4++;
30     }
31     while(cin>>n&&n)
32     {
33         if(n%10==1&&n%100!=11)
34             cout << "The "<<n<<"st humble number is "<<a[n]<<"."<<endl;
35         else if(n%10==2&&n%100!=12)
36             cout << "The "<<n<<"nd humble number is "<<a[n]<<"."<<endl;
37         else if(n%10==3&&n%100!=13)
38             cout << "The "<<n<<"rd humble number is "<<a[n]<<"."<<endl;
39         else
40             cout << "The "<<n<<"th humble number is "<<a[n]<<"."<<endl;
41     }
42     return 0;
43 }
View Code

 

 

标签:丑数,12,int,humble,number,输入
来源: https://www.cnblogs.com/qing123tian/p/11107459.html