码迷,mamicode.com
首页 > 其他好文 > 详细

ACM1005,纸币问题

时间:2016-03-26 17:25:20      阅读:199      评论:0      收藏:0      [点我收藏+]

标签:

问题描述:"Yakexi, this is the best age!" Dong MW works hard and get high pay, he has many 1 Jiao and 5 Jiao banknotes(纸币), some day he went to a bank and changes part of his money into 1 Yuan, 5 Yuan, 10 Yuan.(1 Yuan = 10 Jiao)
"Thanks to the best age, I can buy many things!" Now Dong MW has a book to buy, it costs P Jiao. He wonders how many banknotes at least,and how many banknotes at most he can use to buy this nice book. Dong MW is a bit strange, he doesn‘t like to get the change, that is, he will give the bookseller exactly P Jiao.

 Input
T(T<=100) in the first line, indicating the case number. T lines with 6 integers each: P a1 a5 a10 a50 a100 ai means number of i-Jiao banknotes. All integers are smaller than 1000000.
 
Output
Two integers A,B for each case, A is the fewest number of banknotes to buy the book exactly, and B is the largest number to buy exactly.If Dong MW can‘t buy the book with no change, output "-1 -1".
 
Sample Input
3
33 6 6 6 6 6
10 10 10 10 10 10
11 0 1 20 20 20
 
Sample Output
6 9
1 10
-1 -1
 
意思:有1角,5角,10角,50角,100角的纸币,一本书x角,求花费纸币的最多数量和最小数量
 
思路:一种想法:因为我们要求的是花的最多数量纸币,所以就是要保证手上的纸币数量最少!!这样想的话问题就比较简单了,就转化为最少数量问题了。假设手上总共有p毛,而价格为q毛,我们用手上最少的数量的纸币去凑(p-q)毛,然后再用总数量减去该最少数量即可。
 
代码:

#include <iostream>

using namespace std;
int a[6]={0,1,5,10,50,100};
int main()
{
  int price = 0;//定义买书所需要的钱数;
  int i,j,k;
  int num = 0;//买书的次数
  cin >> num;  
  while(num--)
  {
    int sum = 0;
    int b[6],c[6],d[6];
    cin >> price;
    int t = price;
    for(i = 1;i <= 5;i++)
    {
      cin >> b[i];
      sum += b[i]*a[i];
    }
  //求最少纸币
    for(i=5;i>0;i--)
    {
      if(price/a[i]<b[i])
      {
        c[i]=price/a[i];
        price=price-a[i]*c[i];
      }
    else
    {
      c[i]=b[i];
      price=price-c[i]*a[i];
    }
  }
  if(price != 0)
  {
    cout << "-1" << " " << "-1" << endl;
  }
  else
  {
  //求最大纸币数,反着求
  k = sum - t;//卖完应该应该剩余多少钱
  for(i = 5;i > 0;i--)
  {
    if(k/a[i] < b[i])
    {
      d[i] = k/a[i];
      k = k - d[i] * a[i];
    }
    else
    {
      d[i] = b[i];
      k = k - d[i] * a[i];
    }
  }
  int m1 = 0,m2 = 0,m3 = 0;
  for(i = 1;i <= 5;i++)
  {
    m1 += c[i];
    m2 += d[i];
    m3 += b[i];
  }
  if(k == 0)
    cout << m1 << " " << m3-m2 << endl;
  }

 }
return 0;
}

参考思路:http://www.cnblogs.com/der5820/p/3907645.html

ACM1005,纸币问题

标签:

原文地址:http://www.cnblogs.com/2016zhanggang/p/5323167.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!