UVA136解题报告

先来份错误代码,亲爱的小伙伴们,希望你们能找到其中的问题,顺便说一句,用的是广搜

#include<cstdio>
#include<queue>
using namespace std;

int main()
{
    queue<long long> q;
    int ans=1;
    q.push(1);
    while(ans!=1500)
    {
        long long x=q.front();
        q.pop();
        q.push(x*2); q.push(x*3); q.push(x*5);
        ans++;
    }
    printf("%d\n",q.front());
    return 0;
}

上面是我用很短的时间写出来的很漂亮的代码,如果说有什么不完美的地方,那就是他是错的。

有两点决定了他是错的,一,没有从小到大排序,这样实际上并不能确定谁是第1500个丑数,改进方法是用优先队列

二,同一个丑数可以有不同的生成方式,没有现判断该丑数是否已生成是第二个错误,改进方法是加入set判断

AC代码如下 time 0ms

#include<cstdio>
#include<queue>
#include<set>
#include<vector>
using namespace std;

typedef long long LL;
int d[]={2,3,5};

int main()
{
    set<long long> s;
    priority_queue<LL,vector<LL>,greater<LL> > q;
    int ans=1;
    s.insert(1);
    q.push(1);
    while(ans!=1500)
    {
        long long x=q.top();
        q.pop();
        for(int i=0;i<3;i++)
        {
            long long y=x*d[i];
            if(!s.count(y)) { s.insert(y); q.push(y); }
        }
        ans++;
    }
    printf("The 1500'th ugly number is %lld.\n",q.top());
    return 0;
}
 
再来份搞笑版的代码,可以AC 哦
 
#include<cstdio>
using namespace std;

int main()
{
    printf("The 1500'th ugly number is 859963392.\n");
    return 0;
}



  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 7
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值