Va 674 coin change

Source: Internet
Author: User

AlthoughCodeShort, but there are still many places worth learning. The dynamic pattern mainly refers to the state transition equation. Not yet written. For more information, see http://blog.csdn.net/tclh123/article/details/6286833.

At the same time, pay attention to avoid duplication. One is through the largest coin, the For Loop in 50, 25, 10, 5, 1, DP is also from I = A to I = 0, mark the VIS array to avoid repeated computation and save time.

# Include <cstdio> # include <cstring> # include <iostream> # define maxn 7490 using namespace STD; int coin [] = {50, 25, 1 }; int vis [5] [maxn], d [5] [maxn]; int dp (int A, int money) {If (money = 0) return 1; if (vis [a] [money]) return d [a] [money]; vis [a] [money] = 1; int ans = 0; for (INT I = A; I <5; I ++) if (money> = coin [I]) ans + = dp (I, money-coin [I]); d [a] [money] = ans; return ans;} int main () {// freopen ("in.txt", "r", stdin); int Money; while (scanf ("% d", & money )! = EOF) {// memset (VIS, 0, sizeof (VIS); cout <dp (0, money) <Endl ;}return 0 ;}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.