- #include<iostream>
- #include<algorithm>
- using namespace std;
- const int factors[] = { 2, 3, 5, 7 };
- const int m = 2000000000;
- const int mm[] = { m/2, m/3, m/5, m/7 };
- const char* st = "st", *nd = "nd", *rd = "rd", *th = "th";
- int result[6000];
- int cnt;
- void d(int i, int x)
- {
- if ( x > mm[i] ) return;
- int xl = x;
- while ( xl <= mm[i] )
- {
- if (i < 3 && xl <= mm[i+1])
- d(i+1, xl);
- xl *= factors[i];
- result[cnt++] = xl;
- }
- }
- int main()
- {
- result[0] = 1;
- cnt = 1;
- d(0, 1);
- //cout<<cnt<<endl;
- sort(result, result+cnt);
- int idx;
- const char* postfix;
- while ( (cin>>idx) && idx )
- {
- int mod = idx % 10, mod2 = idx % 100;
- postfix = th;
- switch(mod)
- {
- case 1:
- if (mod2 != 11)
- postfix = st;
- break;
- case 2:
- if (mod2 != 12)
- postfix = nd;
- break;
- case 3:
- if (mod2 != 13)
- postfix = rd;
- break;
- default:
- break;
- }
- cout<<"The "<<idx<<postfix<<" humble number is "<<result[idx-1]<<"."<<endl;
- }
- }
zoj 1095 Humble Numbers
最新推荐文章于 2017-06-21 05:52:28 发布