zoukankan      html  css  js  c++  java
  • USACO2.3.4Money Systems

    Money Systems

    The cows have not only created their own government but they have chosen to create their own money system. In their own rebellious way, they are curious about values of coinage. Traditionally, coins come in values like 1, 5, 10, 20 or 25, 50, and 100 units, sometimes with a 2 unit coin thrown in for good measure.

    The cows want to know how many different ways it is possible to dispense a certain amount of money using various coin systems. For instance, using a system of {1, 2, 5, 10, ...} it is possible to create 18 units several different ways, including: 18x1, 9x2, 8x2+2x1, 3x5+2+1, and many others.

    Write a program to compute how many ways to construct a given amount of money using supplied coinage. It is guaranteed that the total will fit into both a signed long long (C/C++) and Int64 (Free Pascal).

    PROGRAM NAME: money

    INPUT FORMAT

    The number of coins in the system is V (1 <= V <= 25).

    The amount money to construct is N (1 <= N <= 10,000).

    Line 1: Two integers, V and N
    Lines 2..: V integers that represent the available coins (no particular number of integers per line)

    SAMPLE INPUT (file money.in)

    3 10
    1 2 5
    

    OUTPUT FORMAT

    A single line containing the total number of ways to construct N money units using V coins.

    SAMPLE OUTPUT (file money.out)

    10
    题解:其实这个就是一个完全背包问题。所以直接用完全背包的公式即可。
    f[j]=f[j]+f[j-c[i]]。
    View Code
     1 /*
     2 ID:spcjv51
     3 PROG:money
     4 LANG:C
     5 */
     6 #include<stdio.h>
     7 int main(void)
     8 {
     9     freopen("money.in","r",stdin);
    10     freopen("money.out","w",stdout);
    11     long long f[10005];
    12     int i,j,v,n;
    13     int c[30];
    14     scanf("%d%d",&n,&v);
    15     for(i=1;i<=n;i++)
    16     scanf("%d",&c[i]);
    17     memset(f,0,sizeof(f));
    18     f[0]=1;
    19     for(i=1;i<=n;i++)
    20     for(j=c[i];j<=v;j++)
    21         f[j]+=f[j-c[i]];
    22     printf("%lld\n",f[v]);
    23     return 0;
    24 }
  • 相关阅读:
    剑指offer面试题17:合并两个排序的链表
    剑指offer面试题16:反转链表
    剑指offer面试题15:链表中倒数第K个节点
    Jinja2.template渲染两种常见用法
    hadoop集群运维碰到的问题汇总
    hbase配置参数总结
    hbase内核学习总结
    zookeeper学习笔记
    mongodb 3.2性能测试
    kafka内部结构笔记
  • 原文地址:https://www.cnblogs.com/zjbztianya/p/2908966.html
Copyright © 2011-2022 走看看