hdu 1058

题意:求素因子只有2 3 5 7的数
zsd:5842 各种打表
#include<iostream> #include<cstring> using namespace std; __int64 a[6000]; int main() { int n; memset(a,0,sizeof(a)); __int64 c=3000000000; a[1]=1;a[2]=2;a[3]=3;a[4]=4; for(int i=5;i<=5842;i++) { c=3000000000; for(int j=1;j<i;j++) { if(a[j]*2>a[i-1]&&a[j]*2<c) c=a[j]*2; else if(a[j]*3>a[i-1]&&a[j]*3<c) c=a[j]*3; else if(a[j]*5>a[i-1]&&a[j]*5<c) c=a[j]*5; else if (a[j]*7>a[i-1]&&a[j]*7<c) c=a[j]*7; else continue; } a[i]=c; } while(cin>>n&&n) { if(n % 10 == 1 && n % 100 != 11) printf("The %dst humble number is %lld. ",n ,a[n]); else if(n % 10 == 2 && n % 100 != 12) printf("The %dnd humble number is %lld. ",n ,a[n]); else if(n % 10 == 3 && n % 100 != 13) printf("The %drd humble number is %lld. ",n ,a[n]); else printf("The %dth humble number is %lld. ",n ,a[n]); } return 0; }
方法2: 比较经典
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
long long num[60000];
int b[4]={2,3,5,7};
long long  min(long long a,long long b,long long c,long long d)
{
    a=a>b?b:a;
    c=c>d?d:c;
    return a>c?c:a;
}
int main()
{
    num[1]=1;
    int i;
    int l1=1,l2=1,l3=1,l4=1;
    for(i=2;i<=5842;i++)
    {
        num[i]=min(num[l1]*2,num[l2]*3,num[l3]*5,num[l4]*7);
        if(num[i]==num[l1]*2) l1++;
        if(num[i]==num[l2]*3) l2++;
        if(num[i]==num[l3]*5) l3++;
        if(num[i]==num[l4]*7) l4++;
    }
    int n;
    while(scanf("%d",&n),n)
    {
        if(n % 10 == 1 && n % 100 != 11)
              printf("The %dst humble number is %lld. ",n ,num[n]);
          else if(n % 10 == 2 && n % 100 != 12)
              printf("The %dnd humble number is %lld. ",n ,num[n]);
          else if(n % 10 == 3 && n % 100 != 13)
              printf("The %drd humble number is %lld. ",n ,num[n]);
          else
              printf("The %dth humble number is %lld. ",n ,num[n]);
    }
    return 0;
}
原文地址:https://www.cnblogs.com/zhangdashuai/p/3704480.html