2014年3月6日 星期四

UVa OJ - 674 Coin Change

The following program is my ACcepted code for UVA-674 .
It's a for everybody to learn and discuss.
If there is any mistake or comment, please let me know.  :D

此乃UVA 674  的AC code!
歡迎一同討論學習,如有錯誤與任何建議請留言 : )


我之後應該會寫一小篇來解釋這種題目的 DP 思路
但可能要等一小段時間了,如果很想知道的話可以留言,我會趕快寫
I think I will write a small article to explain this problem's DP thought,
but that will in the future some time(?).
If you want to know about that, please leave a commend and I will work on it soon.

此題目可以使用int,因為他的n值比較小。
This problem can be solved when you use int because its n may be smaller.

//This program is for UVA 674 Coin Change
//題目來源 Problem link: http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=8&page=show_problem&problem=615

#include<stdio.h>
#include<string.h>

int main()
{
    int way[8000];
    int n;
    int coin[5] = {1, 5, 10, 25, 50};
 
    while(scanf("%d", &n) != EOF)
    {
        memset(way, 0, sizeof(way));
     
        way[0] = 1;
     
        for(int i = 0; i < 5; i++)
            for(int j = coin[i]; j <= n; j++)
                way[j] = way[j] + way[j - coin[i]];
         
        printf("%d\n", way[n]);
    }
    return 0;
}


Please feel free to use it after adding this blog as an reference. (http://autekroy.blogspot.tw) If there is any mistake or comment, please let me know. :D 

歡迎使用與分享任何內容,但請記得標示此部落格為出處。(http://autekroy.blogspot.tw/) 如果有發現任何的錯誤與建議請留言或跟我連絡。 : )

沒有留言:

張貼留言

請留下您的任何想法或建議!
Please leave any thought or comment!