• <ins id="pjuwb"></ins>
    <blockquote id="pjuwb"><pre id="pjuwb"></pre></blockquote>
    <noscript id="pjuwb"></noscript>
          <sup id="pjuwb"><pre id="pjuwb"></pre></sup>
            <dd id="pjuwb"></dd>
            <abbr id="pjuwb"></abbr>

            ArcTan

            dfs
            隨筆 - 16, 文章 - 117, 評論 - 6, 引用 - 0
            數據加載中……

            白書上的動態規劃D---dp組合計數問題

            Coin Change 

            Suppose there are 5 types of coins: 50-cent, 25-cent, 10-cent, 5-cent, and 1-cent. We want to make changes with these coins for a given amount of money.


            For example, if we have 11 cents, then we can make changes with one 10-cent coin and one 1-cent coin, two 5-cent coins and one 1-cent coin, one 5-cent coin and six 1-cent coins, or eleven 1-cent coins. So there are four ways of making changes for 11 cents with the above coins. Note that we count that there is one way of making change for zero cent.


            Write a program to find the total number of different ways of making changes for any amount of money in cents. Your program should be able to handle up to 7489 cents.

            Input 

            The input file contains any number of lines, each one consisting of a number for the amount of money in cents.

            Output 

            For each input line, output a line containing the number of different ways of making changes with the above 5 types of coins.

            Sample Input 

            11 26 

            Sample Output 

            4 13 

            題目大意:

             給定1 5 10 25 50五種硬幣,給定一個數,問用這些硬幣有多少種不同的組合方式?

            想想好像可以是一個方程組求解的問題嘛:

                            x1+5*x2+10*x3+25*x4+50*x5=x0;

                            給定x0,一組滿足條件的x>=0就是解集。啊哈哈哈。可以高斯消元法咯。

            不過,明顯的計數問題:

            dp[i][j]表示i可以由前j種硬幣表示的方法數。

            dp[i][j]=dp[i][j-1]+dp[i-coin[j]][j]     i-coin[j]>0

            dp[i][j]=dp[i][j-1]+1  i-coin[j]=0;

            dp[i][j]=dp[i][j-1] i-coin[j]<0

            總結:

                  方程類問題,一定要先把方程寫清楚!!!

            #include<stdio.h>
            #include<string.h>
            #include<math.h>
            long long dp
            [7500][5],coin[5]={1,5,10,25,50};
            int GetAns()
            {
                int i
            ,j;
                memset(dp,0,sizeof(dp));
                dp[1][0]=dp[1][1]=dp[1][2]=dp[1][3]=dp[1][4]=1;
                for (i=2;i<=7489 ;i++ )
                {
                    dp
            [i][0]=1;
                    for (j=1;j<5 ;j++ )
                        if (i-coin[j]>0)
                            dp
            [i][j]=dp[i][j-1]+dp[i-coin[j]][j];
                        else
                            if (i-coin
            [j]==0)
                                dp
            [i][j]=dp[i][j-1]+1;
                            else
                                dp
            [i][j]=dp[i][j-1];
                }
            }
            int main()
            {
                int n
            ;
                GetAns();
                while (scanf("%d",&n)==1)
                    printf(
            "%lld\n",dp[n][4]);
                return 0;
            }


            額額,dp是個好東西,該看看優化了!!!

            posted on 2012-04-29 19:11 wangs 閱讀(852) 評論(0)  編輯 收藏 引用 所屬分類: ACM-DP

            久久精品成人欧美大片| 国产精品成人精品久久久| 成人综合久久精品色婷婷| 亚洲AV无码成人网站久久精品大| 亚洲精品无码久久久影院相关影片| 国内精品久久久久影院优| 久久亚洲AV无码西西人体| 亚洲AV无码久久精品狠狠爱浪潮 | 久久亚洲美女精品国产精品| 97久久久久人妻精品专区 | 99久久精品免费看国产一区二区三区 | 99国产欧美精品久久久蜜芽| 欧美国产精品久久高清| 欧美大香线蕉线伊人久久| 久久国产成人| 91视频国产91久久久| 午夜天堂精品久久久久| 一日本道伊人久久综合影| 99久久综合国产精品二区| 久久er99热精品一区二区| 久久久久99这里有精品10 | 精品久久香蕉国产线看观看亚洲 | 伊人久久国产免费观看视频 | 国产福利电影一区二区三区久久老子无码午夜伦不 | 色综合久久综合网观看| 久久久久国产精品嫩草影院| 成人精品一区二区久久久| 久久超乳爆乳中文字幕| 亚洲国产精品无码久久一线| 久久热这里只有精品在线观看| 开心久久婷婷综合中文字幕| 久久久久国色AV免费观看| 久久成人18免费网站| 久久综合五月丁香久久激情| 亚洲国产小视频精品久久久三级 | 久久夜色精品国产| 精品久久久久久国产三级| 久久99中文字幕久久| 97久久精品午夜一区二区| 99久久国产综合精品网成人影院| 久久成人国产精品二三区|