poj2247Humbles Numbers

来源:互联网 发布:苹果显示器知乎 编辑:程序博客网 时间:2024/05/22 01:32
/*看题就知道什么意思了,必须是2,3,5,7这几个数的倍数。。这题的思路就是先把所有的都存到一个数组,不要输一个数算一个。
还有一个难度是这数怎么求。这四个数的倍数从小到大排列。用一个比较大小的函数,返回最小值,,设置i1,i2,i3,i4这几个变量当
数组a的下标分别和2 3 5 7相乘,最小数赋值a[i], 然后if判断和a[i]相等的下标+1,,输出时注意后两位11,12,13的不一样*/

#include<iostream>#include<algorithm>using namespace std;int min_4(int a, int b, int c, int d){    int t[4]; t[1] = a; t[2] = b; t[3] = c; t[0] = d;    sort(t, t + 4);    return t[0];}int main(){    int a[5843];    int i1 = 1, i2 = 1, i3 = 1, i4 = 1, i;    a[1] = 1;    for (i = 2; i < 5843; ++i)                    //2.3.5.7的倍数的数的求法。    {        a[i] = min_4(a[i1] * 2, a[i2] * 3, a[i3] * 5, a[i4] * 7);        if (a[i] == a[i1] * 2) ++i1;        if (a[i] == a[i2] * 3) ++i2;        if (a[i] == a[i3] * 5) ++i3;        if (a[i] == a[i4] * 7) ++i4;    }    int m;    while (cin >> m&&m)    {        if (m%100 == 11||m%100 ==12||m%100==13)            cout << "The "<<m<<"th humble number is "<<a[m]<<"." << endl;        else if (m%10 == 1)            cout << "The "<<m<<"st humble number is "<<a[m]<<"." << endl;        else if (m%10 ==2)            cout << "The " << m << "nd humble number is " << a[m] << "." << endl;        else if (m % 10 == 3)            cout << "The " << m << "rd humble number is " << a[m] << "." << endl;        else            cout << "The " << m << "th humble number is " << a[m] << "." << endl;    }    return 0;}


/*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.
*/
0 0