HDU 1058 Humble Numbers

集训过半了,都开始 个人积分赛了,还是这么弱怎么办哟。。。起步晚了什么都不知道有木有,暑假集训才开始看LRJ  的小白书,才开始看小白书,才开始接触OJ 刷题。。。噗,已经被人家甩的好远了 好了好了 不吐槽了  ----------------------------------------------------------------------

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1058

题意就不解释了,这一份是 http://blog.sina.com.cn/s/blog_7b7c7c5f0100ss70.html    借鉴这个大神的代码

#include <iostream>
#include <string>
#include <algorithm>
#include <stack>
#include <queue>
#include <stack>
#include <map>
#include<set>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <cmath>

using namespace std;

int main()
{
    int i,j,k,l,p;
    int a[6000];
    a[1]=1;
    i=j=k=l=1;
    for(p=2;p<6000;p++)
    {
        a[p]=min(a[i]*2,min(a[j]*3,min(a[k]*5,a[l]*7) ));
        if(a[p]==a[i]*2)
                i++;
        if(a[p]==a[j]*3)
            j++;
        if(a[p]==a[k]*5)
            k++;
        if(a[p]==a[l]*7)
            l++;
    }
    int n;
    while(~scanf("%d",&n),n)
    {
        printf("The %d",n);
//        if(n%10==1) printf("st ");
//        if(n%10==1 && n%100!= 11) printf("st ")
//        else if(n%10==2) printf("nd ");
//        else if(n%10==3) printf("rd ");
//        else  printf("th ");

        if(n%10==1 && n%100 != 11) printf("st ");
        else if(n%10==2 && n%100 != 12) printf("nd ");
        else if(n%10==3 && n%100 != 13) printf("rd ");
        else printf("th ");

        printf("humble number is %d.\n",a[n]);
    }
    return 0;
}

之前没考虑n%100==1 (就是n=121,131 这种情况,这样子还是以 th 结尾而不是st) WA 了一次


下面这个代码是用ZSC学长STL 优先队列写的,我个人由于目前能力不够而且比较懒,特别喜欢用STL 里面模板和算法,再说,能把STL 用活用熟也是一个很大的本事 

#include<cstdio>
#include<cstring>
#include<cmath>
#include<vector>
#include<stack>
#include<queue>
#include<map>
#include<cmath>
#include<string>
#include<algorithm>
using namespace std;

typedef __int64 LL;

#define lson (n<<1)
#define rson (n<<1|1)

int Max(int a,int b) { return a>b?a:b; }
int Min(int a,int b) { return a<b?a:b; }

const int N = 5842, K=52;
const int INF = 1e9;
const int MOD = 3;
const double Eps = 1e-7;

struct Node{
    LL num;
    bool operator < (const Node a) const {
        return num > a.num;
    }
}nod;
priority_queue<Node> q;
map<int,int> vis;
LL ans[N];
int main()
{
//    freopen("in.txt", "r", stdin);
    while(!q.empty()) q.pop();
    vis.clear();
    nod.num = 1;
    q.push(nod);
    int cnt=1;
    for(int i=1; cnt<=N; i++){
        nod = q.top(); q.pop();
        if(vis[nod.num]) continue;
        vis[nod.num]=1;
        ans[cnt] = nod.num;
        q.push((Node){ans[cnt]*2});
        q.push((Node){ans[cnt]*3});
        q.push((Node){ans[cnt]*5});
        q.push((Node){ans[cnt]*7});
        cnt++;
    }
    int n;
    while(~scanf("%d",&n)&&n){
		printf("The ");
		if(n%100==11||n%100==12||n%100==13) {
            printf("%dth humble number is %I64d.\n",n,ans[n]);
            continue;
        }
		if(n%10==1)printf("%dst",n);
		else if(n%10==2)printf("%dnd",n);
		else if(n%10==3)printf("%drd",n);
		else printf("%dth",n);
		printf(" humble number is %I64d.\n",ans[n]);
    }
    return 0;
}



再下面这个呢,就是我自己写的,我用的 set 容器,set 容器有去重性和自动排列   这个只是写来试验的,没有按照题目要求输出,要用的话,改一下输出就好了


#include <iostream>
#include <string>
#include <algorithm>
#include <stack>
#include <queue>
#include <stack>
#include <map>
#include<set>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <math.h>
using namespace std;
set<int >s;
int a[8000];
int main()
{
    //freopen("666.txt","w",stdout);
    s.insert(1);
    set<int>::iterator it;
    int i=1;
	int ans[6000];
	ans[1]=1;
    for(it =s.begin(); it !=s.end(); it++)
    {
        s.insert(*it*2);
        s.insert(*it*3);
        s.insert(*it*5);
        s.insert(*it*7);
        ans[i++]=*it;
        if(i >=5842)
            break;
    }
	for(i=1; i<=20; i++) printf("%d ",ans[i]);
    //for(it=s.begin(); it !=s.end(); it++)
    //cout<<*it<<",";
    return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值