ACM1005,纸币问题

问题描述:"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

时间: 2024-08-10 01:53:40

ACM1005,纸币问题的相关文章

如何辨别第一套人民币牧马图纸币1万元真假

在收藏人民币的过程中,对投资者来说,最惨淡的事情,莫过于收到假币了.对于第一套人民币中的壹万圆牧马纸币的真假该如何判断呢?外汇返佣有妙招. 第一套人民币中的壹万圆牧马纸币于1951年5月17日发行,在1955年4月1日停止流通,它约是第一套大全套价格的50%左右,是第一套人民币中价值最高的纸币,有中国人民币之宝和票王之称. 辨别第一套人民币中的壹万圆牧马纸币真假的方法: 方法一: 由于印章和号码是当时用数百吨冲压力压制成,因此有“过底”现象(即可透到另一面),使用五色胶印工艺,真钞经过五次套印,

纸币时代的终结—2015上半年经济综述

纸币时代的终结—2015上半年经济综述 2015-07-20 18:19阅读 10万+ 本文的天涯阅读地址:http://bbs.tianya.cn/post-develop-2045348-1.shtml 纸币时代的终结—2015上半年经济综述 写在前面的话 这些年来,每年年中一次,年底一次,讲述宏观经济,已经成了我的惯例.作为一名离经叛道的乐观主义者,我总是要不停的突破极限,阐述更加深刻的道理,描绘更为宏大的历史,刻画更为广阔的未来.这一次,相信也不会让大家失望. 有句话,在我这两年的总结中

数字货币:从纸币泡沫看比特币泡沫,从互联网泡沫看区块链泡沫

目录 货币:一种观念,一个记账单位 纸币:从泡沫到法币,从自由到垄断 数字货币:6000年后,回到起点 互联网:从泡沫到革命,从1.0到4.0 区块链:从泡沫到-- 为了不引起读者混淆,笔者在一些概念上作了区分.数字货币:通指一种新的货币制度,类似于信用货币制度或者金本位制度等:虚拟币:指代以比特币.莱特币等为代表的由市场创造的虚拟代币:同时提醒读者注意货币的虚拟化和虚拟币的意思也不一样,前者主要指现在的纸币虚拟化/数字化的过程. 一.货币:一种观念 雅浦岛和费币的故事在学界广为人知,货币学派的

杭电ACM1005

Number Sequence Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 153662    Accepted Submission(s): 37490 Problem Description A number sequence is defined as follows: f(1) = 1, f(2) = 1, f(n) = (A

hd acm1005

问题: 已知递推公式:f[i] = ( a * f[i-1] + b * f[i-2] ) % 7,f[1]=1,f[2]=1. 需要你输入三个数a,b,n.其中a,b用来补充上述公式,用补充后的公式计算 f[n]. 析: 每次输入a,b都会有一个对应的公式,由这个公式会得到的一个周期性的数列,然后利用这个周期来计算f[n]便可以了.所以如何找到周期是重点,因为每个周期开始时前两个数字会是1,1,这可以是一个标志,当然别的标志也可以:另外需要注意一点,如果a,b都能整除7,那么从f[3]向后都会

GDUFE ACM-1005

Digital Roots Time Limit: 2000/1000ms (Java/Others) Problem Description: The digital root of a positive integer is found by summing the digits of the integer. If the resulting value is a single digit then that digit is the digital root. If the result

给你六种面额1 5 10 20 50 100元的纸币假设每种币值的数量足够多

编写程序求组成N元的不同组合的个数.输入一个数字N输出一个也是数字,为组成N的组合个数.如输入2,输出1,输入5,输出2 背包问题: 1 function fn (all) { 2 const arr = [1, 5, 10, 20, 50, 100], 3 len = arr.length, 4 res = []; 5 for (let i = 0; i <= len; i++) { 6 res[i] = []; 7 res[i][0] = 1; 8 } 9 for (let j = 1; j

ACM1005:Number Sequence

Problem Description A number sequence is defined as follows:f(1) = 1, f(2) = 1, f(n) = (A * f(n - 1) + B * f(n - 2)) mod 7.Given A, B, and n, you are to calculate the value of f(n). Input The input consists of multiple test cases. Each test case cont

JS循环

JS循环基础知识 [循环结构的执行步骤] 1,声明循环变量: 2,判断循环条件: 3,执行循环体操作: 4,更新循环变量: 然后,循环执行2-4,直到条件不成立时,跳出循环: while循环()中的表达式,运算结果可以是各种类型.但是最终都会转化为真假,转换规则同if结构: ①boolean:true为真,flase为假: ②string:空字符串为假,所有非空字符串为真: ③number:0为假,一切非0数字为真: ④null,undefined,NaN全为假: ⑤object:全为真: wh