HDU 1058 Humble Numbers

参考过网上的题解才AC的。寻找丑数的过程十分奇妙。主要是有个pos[4]数组记录下标移动的情况。保证答案数组ans[N]是从小到大升序且无重复的排列。这个世界真奇妙真奇妙真奇妙啊。膜拜想出来此方法的大神,请接受小女子的膝盖Orz…


需要注意的地方是输出的时候关于11,12,13,111,112,113.。的问题、

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
const int N = 5842+10;
int ans[N];
void init()
{
    int temp[4]={2,3,5,7};
    int pos[4]={1,1,1,1};//奇妙的地方在这里
    ans[1]=1;
    for(int i=2;i<=5842;i++)
    {
        int a[4];
        for(int i=0;i<4;i++) a[i]=temp[i]*ans[pos[i]];
        int x=2000000000+10;
        for(int i=0;i<4;i++) x=min(x,a[i]);//找最小值更新保证数组从小到大的顺序
        for(int i=0;i<4;i++) if(x==a[i])  pos[i]++;//去重的地方
        ans[i]=x;//直接更新
    }
    return;
}
int main()
{
    init();
    int n;
    while(~scanf("%d",&n))
    {
        if(n==0) break;
        if(n%10==1 && n%100!=11) printf("The %dst humble number is %d.\n",n,ans[n]);//需要注意的地方!不然你会WA到爽!
        else if(n%10==2 && n%100!=12) printf("The %dnd humble number is %d.\n",n,ans[n]);
        else if(n%10==3 && n%100!=13) printf("The %drd humble number is %d.\n",n,ans[n]);
        else printf("The %dth humble number is %d.\n",n,ans[n]);
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值