zoukankan      html  css  js  c++  java
  • ZOJ 2734 Exchange Cards

                  Exchange Cards

    Problem Description

    As a basketball fan, Mike is also fond of collecting basketball player cards. But as a student, he can not always get the money to buy new cards, so sometimes he will exchange with his friends for cards he likes. Of course, different cards have different value, and Mike must use cards he owns to get the new one. For example, to get a card of value 10$, he can use two 5$ cards or three 3$ cards plus one 1$ card, depending on the kinds of cards he have and the number of each kind of card. And Sometimes he will involve unfortunately in a bad condition that he has not got the exact value of the card he is looking for (fans always exchange cards for equivalent value).

    Here comes the problem, given the card value he plans to get and the cards he has, Mike wants to fix how many ways he can get it. So it's you task to write a program to figure it out.

    Input

    The problem consists of multiple test cases, terminated by EOF. There's a blank line between two inputs.

    The first line of each test case gives n, the value of the card Mike plans to get and m, the number of different kinds of cards Mike has. n will be an integer number between 1 and 1000. m will be an integer number between 1 and 10.

    The next m lines give the information of different kinds of cards Mike have. Each line contains two integers, val and num, representing the value of this kind of card, and the number of this kind of card Mike have.

    Note: different kinds of cards will have different value, each val and num will be an integer greater than zero.

    Output

    For each test case, output in one line the number of different ways Mike could exchange for the card he wants. You can be sure that the output will fall into an integer value.

    Output a blank line between two test cases.

    Sample Input

    5 2
    2 1
    3 1
    
    10 5
    10 2
    7 2
    5 3
    2 2
    1 5
    

    Sample Output

    1
    
    7
     1 #include<cstdio>
     2 #include<cstring>
     3 using namespace std;
     4 
     5 int n,m;
     6 
     7 struct card
     8 {
     9     int val;
    10     int num;
    11 }p[15];
    12 
    13 int cnt;
    14 
    15 void dfs(int cur,int total,int tar)
    16 {
    17     if(total==tar)
    18     {
    19         cnt++;
    20         return;
    21     }
    22     if(total>tar||cur==m)
    23     return;
    24     for(int i=0;i<=p[cur].num;i++)
    25     dfs(cur+1,total+i*p[cur].val,tar);
    26 }
    27 
    28 int main()
    29 {
    30     bool flag=false;
    31     while(scanf("%d%d",&n,&m)!=EOF)
    32     {
    33         if(flag)
    34         printf("
    ");
    35         for(int i=0;i<m;i++)
    36         scanf("%d%d",&p[i].val,&p[i].num);
    37         cnt=0;
    38         dfs(0,0,n);
    39         printf("%d
    ",cnt);
    40         flag=true;
    41     }
    42     return 0;
    43 }
  • 相关阅读:
    等待队列设备[置顶] Linux设备驱动,等待队列
    宠物功能[置顶] QQ宠物保姆
    选中拖动Unity3D系列教程–使用免费工具在Unity3D中开发2D游戏 第二节(下)
    序列化对象java中为什么要实现序列化,什么时候实现序列化?
    函数表达式[置顶] 母函数详解
    文件问题cocos2dx&cocosbuilder折腾记
    模块functionJavaScript学习笔记(二十五) 沙箱模式
    nullnullflume ng配置拓扑图
    对象序列化对象的序列化和反序列化
    扩展编程PHP自学之路PHP数据库编程
  • 原文地址:https://www.cnblogs.com/homura/p/4703496.html
Copyright © 2011-2022 走看看