hdu 1058 Humble Numbers (DP初步)

题意:

       一组质因子只含有2,3,5,7的数据从小到大排列,求出第n个数

解题思路:

          1.此题为动态规划,难点在于如何将数据从小到大放在数组中

          2.各个因子逐渐增加相乘,再比较大小得出数组

          3.注意输出格式,

            末尾位数为1,且不为11st

            末尾位数为2,且不为12为nd

            末尾位数为3,且不为13为rd

#include<stdio.h>
int main()
{
    int n;
    int num[5843];
    int p2=0,p3=0,p5=0,p7=0;
    int t,t1,t2;
    num[0] = 1;
    for(int i=1;i<5843;i++)
    {
        t1 = 2*num[p2]>3*num[p3] ? 3*num[p3]:2*num[p2];
        t2 = 5*num[p5]>7*num[p7] ? 7*num[p7]:5*num[p5];
        t=t1<t2 ? t1:t2;
        //if(num[i-1]!=t)num[i]=t;
        //else i--;
        num[i] =t;
        if(t==2*num[p2])p2++;
        if(t==3*num[p3])p3++;//else
        if(t==5*num[p5])p5++;//else
        if(t==7*num[p7]) p7++;//else
    }
    while(scanf("%d",&n)!=EOF&&n!=0)
    {
        if((n-1)%10==0&&n%100!=11)printf("The %dst",n);
        else if((n-2)%10==0&&n%100!=12)printf("The %dnd",n);
        else if((n-3)%10==0&&n%100!=13)printf("The %drd",n);
        else printf("The %dth",n);
        printf(" humble number is %d.\n",num[n-1]);
    }

    return 0;
}

     若在预先初始化数组的代码中中,用if else 语句代替4个if语句,则须按//处修改!!

因为存在2*3和3*2 2和3 的计数器都要加一的情况!!

 

原文地址:https://www.cnblogs.com/XDJjy/p/3002828.html