首页 > 代码库 > hdu 1058

hdu 1058

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
题意:求素因子只有2 3 5 7的数<br>zsd:5842 各种打表<br>#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",n ,a[n]); 
          else if(n % 10 == 2 && n % 100 != 12) 
              printf("The %dnd humble number is %lld.\n",n ,a[n]); 
          else if(n % 10 == 3 && n % 100 != 13) 
              printf("The %drd humble number is %lld.\n",n ,a[n]); 
          else 
              printf("The %dth humble number is %lld.\n",n ,a[n]); 
 
        }
        return 0;
}<br>方法2: 比较经典<br>#include<iostream><br>#include<cstdio><br>#include<cstring><br>#include<algorithm><br>using namespace std;<br>long long num[60000];<br>int b[4]={2,3,5,7};<br>long long  min(long long a,long long b,long long c,long long d)<br>{<br>    a=a>b?b:a;<br>    c=c>d?d:c;<br>    return a>c?c:a;<br>}<br>int main()<br>{<br>    num[1]=1;<br>    int i;<br>    int l1=1,l2=1,l3=1,l4=1;<br>    for(i=2;i<=5842;i++)<br>    {<br>        num[i]=min(num[l1]*2,num[l2]*3,num[l3]*5,num[l4]*7);<br>        if(num[i]==num[l1]*2) l1++;<br>        if(num[i]==num[l2]*3) l2++;<br>        if(num[i]==num[l3]*5) l3++;<br>        if(num[i]==num[l4]*7) l4++;<br>    }<br>    int n;<br>    while(scanf("%d",&n),n)<br>    {<br>        if(n % 10 == 1 && n % 100 != 11)<br>              printf("The %dst humble number is %lld.\n",n ,num[n]);<br>          else if(n % 10 == 2 && n % 100 != 12)<br>              printf("The %dnd humble number is %lld.\n",n ,num[n]);<br>          else if(n % 10 == 3 && n % 100 != 13)<br>              printf("The %drd humble number is %lld.\n",n ,num[n]);<br>          else<br>              printf("The %dth humble number is %lld.\n",n ,num[n]);<br>    }<br>    return 0;<br>}