zoj 1095 Humble Numbers

  1. #include<iostream>
  2. #include<algorithm>
  3. using namespace std;
  4. const int factors[] = { 2, 3, 5, 7 };
  5. const int m = 2000000000;
  6. const int mm[] = { m/2, m/3, m/5, m/7 };
  7. const char* st = "st", *nd = "nd", *rd = "rd", *th = "th";
  8. int result[6000];
  9. int cnt;
  10. void d(int i, int x)
  11. {
  12.     if ( x > mm[i] ) return;
  13.     int xl = x;
  14.     while ( xl <= mm[i] )
  15.     {
  16.         if (i < 3 && xl <= mm[i+1])
  17.                 d(i+1, xl);
  18.         xl *= factors[i];
  19.         result[cnt++] = xl;
  20.     }
  21. }
  22. int main()
  23. {
  24.     result[0] = 1;
  25.     cnt = 1;
  26.     d(0, 1);
  27.     //cout<<cnt<<endl;
  28.     sort(result, result+cnt);
  29.     
  30.     int idx;
  31.     const char* postfix;
  32.     while ( (cin>>idx) && idx )
  33.     {
  34.         int mod = idx % 10, mod2 = idx % 100;
  35.         postfix = th;
  36.         switch(mod)
  37.         {
  38.             case 1:
  39.                 if (mod2 != 11)
  40.                     postfix = st;
  41.                 break;
  42.             case 2:
  43.                 if (mod2 != 12)
  44.                     postfix = nd;
  45.                 break;
  46.             case 3:
  47.                 if (mod2 != 13)
  48.                     postfix = rd;
  49.                 break;
  50.             default:
  51.                 break;
  52.         }
  53.         cout<<"The "<<idx<<postfix<<" humble number is "<<result[idx-1]<<"."<<endl;
  54.     }
  55. }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值