zoukankan      html  css  js  c++  java
  • HDU1058

       一道DP题,按照一个法则来生成一系列数,初始数是1.任意的第i个数可以看成是在i以前的数*2,*3,*5,或*7得到的,因为是按照从小到大的顺序生成的,于是DP[i]=min{2*DP[p1],3*DP[p2],5*DP[p3],7*DP[p4]};p1,p2,p3,p4分别表示用*2,*3,*4,*5方法生成新数的每列原数的最后一个数的下标。比如,假设集合A={a1,a2,a3..ak}表示由*2方法生成新数的原数集合,则当前下p1=k,当下一次用法则*2生成一个新数为2*ak,则更新集合A={a1,a2...ak,ak+1}此时更新p1=k+1;并且集合A一定是原数列Dp[i]的一个连续子列,即为a1=DP[1],a2=Dp[2]...ak=DP[k].所以只需要用指针记住集合A中最后一项位置即可。对于四中不同法则,每次需要取他们中生成数的最小者即可,并将此法则中的指针后移动一位来等待下一个数的生成。

    #include<iostream>
    using namespace std;
    void HumerNumber();
    void Format(int n);
    int DP[5843];
    int Min(int a, int b, int c, int d);
    int main(){
    int n;
    HumerNumber();
    while (cin >> n&&n){
    Format(n);
    cout <<DP[n]<<"."<< endl;
    }
    return 0;
    }
    void Format(int n){
    if (n%100!=11&&n % 10 == 1)
    cout << "The "<<n<<"st humble number is ";
    else if (n%100!=12&&n%10==2)
    cout << "The " << n << "nd humble number is ";
    else if (n%100!=13&&n%10==3)
    cout << "The " << n << "rd humble number is ";
    else
    cout << "The " << n << "th humble number is ";
    }
    int Min(int a, int b, int c, int d){
    int min = a;
    if (b < min)
    min = b;
    if (c < min)
    min = c;
    if (d < min)
    min = d;
    return min;
    }
    void HumerNumber(){
    int p1, p2, p3, p4,i;
    DP[1] = 1;
    p1 = p2 = p3 = p4=1;
    for (i = 2; i <= 5842; i++){
    DP[i] = Min(2 * DP[p1], 3 * DP[p2], 5 * DP[p3], 7 * DP[p4]);
    if (DP[i] == 2 * DP[p1])
    p1++;
    if (DP[i] == 3 * DP[p2])
    p2++;
    if (DP[i] == 5 * DP[p3])
    p3++;
    if (DP[i] == 7 * DP[p4])
    p4++;
    }
    }

  • 相关阅读:
    CodeForces 681D Gifts by the List (树上DFS)
    UVa 12342 Tax Calculator (水题,纳税)
    CodeForces 681C Heap Operations (模拟题,优先队列)
    CodeForces 682C Alyona and the Tree (树上DFS)
    CodeForces 682B Alyona and Mex (题意水题)
    CodeForces 682A Alyona and Numbers (水题,数学)
    Virtualizing memory type
    页面跳转
    PHP Misc. 函数
    PHP 5 Math 函数
  • 原文地址:https://www.cnblogs.com/td15980891505/p/4981390.html
Copyright © 2011-2022 走看看