杭電1058

9429 ワード

    Humble Numbers
http://acm.hdu.edu.cn/showproblem.php?pid=1058 Problem Description A number whose only preme factors are 2,3,5 or 7 is caled a hmble number.The sequence 1,2,3,4,5,6,7,8,10,12,14,15,16,18,20,21,24,27,shows.shows 20
Write a program to find and print the nth element in this sequence
Input The input consists of one or more test cases.Each test case consists of one integer n with 1<=n==5842.Input is terminated by value ofゼロ(0)for n.
Output For each test case,print one line sayng“The n th hmble number is number.”.Depending on the value of n,the corect suffix“st”,“nd”,or“th”for the ordinal number.the puthas the limped。
Sample Input 1 2 3 11 11 11 11 21 23 100 5842 0
Sample OututputThe 1 st hmumle numbes 1.The 2 nd hmumle numbes 2.The 3 rd hmumle numbes 3.The 4 th hmmmumbei 4.The 11 th hmumle numbei 12.The 12.The 12 th hmumle number is 14.The 13 th mmumumumumumumumumumle mbes 23.The 13.The 13 mmmmmumumumumumumumumumumumumumle 23.The 23.The 23.The 23.The 23 mmmmmmmmb.The 23.The 23.The 23 mb.The 23 mmb.The 23 mumumumumumth humle number is 450.The 1000 th humle number is 385875.The 5842 nd hmble number is 2000000.
素数因子が2,3,5または7だけの数を謙遜数といいます。nを入力して、n番目の謙譲虚数を出力します。方法:すべての謙譲数を前処理します。注意:出力の個数は、nが端数11,12,13の時thであり、その他の端数は1,2,3のものがst,nd,rdである。
エラーコード:
# include <iostream>
# include <cstdio>
using namespace std;


long long  num[10000];
int main(){

    int j,k,n,m;
    long long i;
    k = 0;      
    for(i=1;i<=2000000003;i++){
        long long  t = i;
        while(1){
            if(t%2==0){
                t = t/2;
            }else{
                break;
            }
        }

        while(1){
            if(t%3==0){
                t = t/3;
            }else{
                break;
            }
        }

        while(1){
            if(t%5==0){
                t = t/5;
            }else{
                break;
            }
        }

        while(1){
            if(t%7==0){
                t = t/7;
            }else{
                break;
            }
        }



    }

    while(scanf("%d",&n),n!=0){

        int t = n%10;

        if(t==1) printf("The %dst humble number is %d
"
,n,num[n]); else if(t==2) printf("The %dnd humble number is %d
"
,n,num[n]); else if(t==3) printf("The %drd humble number is %d
"
,n,num[n]); else printf("The %dth humble number is %d
"
,n,num[n]); } return 0; }
正しいコード:
# include <iostream>
# include <cstdio>

using namespace std;

int a[100000];
int main(){

    int i,j,k;
    int temp1,temp2;

    a[0] = 1;
    int m2=0,m3=0,m5=0,m7=0;
    for(i=1;i<5842;i++){

        temp1 = 2*a[m2]>3*a[m3]?3*a[m3]:2*a[m2];
        temp2 = 5*a[m5]>7*a[m7]?7*a[m7]:5*a[m5];
        a[i] = temp1>temp2?temp2:temp1;
        if(a[i]==2*a[m2]) m2++;
        if(a[i]==3*a[m3]) m3++;
        if(a[i]==5*a[m5]) m5++;
        if(a[i]==7*a[m7]) m7++; 
    }


    int n;
    while(scanf("%d",&n),n!=0){

        int t = n%10;

        if(t==1&&!(n>10&&n/10%10==1)){
            printf("The %dst humble number is %d.
"
,n,a[n-1]); }else if(t==2&&!(n>10&&n/10%10==1)){ printf("The %dnd humble number is %d.
"
,n,a[n-1]); }else if(t==3&&!(n>10&&n/10%10==1)){ printf("The %drd humble number is %d.
"
,n,a[n-1]); }else { printf("The %dth humble number is %d.
"
,n,a[n-1]); } } return 0; }