zoukankan      html  css  js  c++  java
  • hd 1058 dp

    呵呵呵,这题的话,去年不知道怎么就水过去了,现在做还是懵逼了

    总是感觉这题很奇怪,哎

    2,3,5,7的系数必然在已打出的表中取

    状态转移方程

    dp(n) = min(dp[i]*2,dp[j]*3,dp[k]*5,dp[l]*7)

    i<=j<=k<=l<n,

    a[4]={2,3,5,7}

    用一个一维数组保存下标,cnt[i]记录a[i]所能取得的最大表下标

    1.遍历所有的cnt[i]找最小值 

      mmin为dp[cnt[i]*a[i]]中的最小值

    2.遍历所有的cnt[i],更新cnt[i]

      if dp[cnt[i]]*a[i]==mmin 

        cnt[i]++;

    //5842
    
    #include <iostream>
    #include<cstdio>
    #include <cmath>
    #include <cstring>
    #include <algorithm>
    #include <string>
    
    using namespace std;
    const int inf = (1<<31)-1 ;
    const int MAXN = 6e3;
    int dp[MAXN];
    int a[4]={2,3,5,7};
    int ct[4]={1,1,1,1};
    
    int main()
    {
        int n;
        int mmin;
        dp[1] = 1;
        for(int i=2;i<=5842;i++){
            mmin = inf;
            for(int j=0;j<4;j++){
                mmin = min(mmin,a[j]*dp[ct[j]]);
            }
            dp[i] = mmin;
            for(int j=0;j<4;j++){
                if(dp[i]==a[j]*dp[ct[j]])
                    ct[j]++;
            }
        }
    
        while(scanf("%d",&n),n){
            cout<<"The "<<n;
            if(10<n&&n<20)cout<<"th ";
            else if(n%10==1)cout<<"st ";
            else if(n%10==2)cout<<"nd ";
            else if(n%10==3)cout<<"rd ";
            else cout<<"th ";
            cout<<"humble number is "<<dp[n]<<"."<<endl;
        }
        //cout << "Hello world!" << endl;
        return 0;
    }
    View Code
    在一个谎言的国度,沉默就是英雄
  • 相关阅读:
    WPF-触发器
    WPF使用socket实现简单聊天软件
    git常用命令备忘
    (转载)WPF中的动画——(一)基本概念
    WPF中的依赖项属性
    C#中的索引器
    C#中的装箱拆箱
    编程语言的弱类型、强类型、动态类型、静态类型
    WPF中的数据驱动
    WPF中的命令简介
  • 原文地址:https://www.cnblogs.com/EdsonLin/p/5361891.html
Copyright © 2011-2022 走看看