Humble Numbers参考程序

前二十个Humble Numbers:1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 12, 14, 15, 16, 18, 20, 21, 24, 25, 27……

用c2,c3,c5,c7做指针,a[i]= min(a[c2]*2,a[c3]*3,a[c5]*5,a[c7]*7)

初始状态:a[1]=1

上程序:

#include<bits/stdc++.h>
using namespace std;
long long c2=1,c3=1,c5=1,c7=1,n,a[6000];
string ss;
int main(){
    a[1]=1;
        //FIRST    预处理Humble Numbers,用数组a记录 
    for(int i=2;i<=5842;i++){
        a[i]=min(min(a[c2]*2,a[c3]*3),min(a[c5]*5,a[c7]*7));
            //求出下一个Humble Number 
        if(a[i]==a[c2]*2) c2++;
        if(a[i]==a[c3]*3) c3++;         
        if(a[i]==a[c5]*5) c5++;
        if(a[i]==a[c7]*7) c7++;
            //重复的也要将指针转移到下一个 
    }
    while(scanf("%lld",&n)){
        if(n==0) break;     //多组数据输入
            //SECOND  序数词缩写判定 
        ss="th";
        if(n%10==1) ss="st";
        if(n%10==2) ss="nd";
        if(n%10==3) ss="rd";
        if(n%100==11||n%100==12||n%100==13) ss="th";
            // 11,12,13要%100!!!说多了都是泪 
        cout<<"The "<<n<<ss<<" humble number is "<<a[n]<<'.'<<endl;
            //输出,注意格式 
    }
    return 0;
}

 

posted @ 2020-04-20 13:37  nhflsoiers  阅读(226)  评论(0)    收藏  举报