编程介的小学生 2020-02-07 21:55 采纳率: 0.2%
浏览 142

Exact Change 交换问题

Problem Description

  • Seller: That will be fourteen dollars.
  • Buyer: Here's a twenty.
  • Seller: Sorry, I don't have any change.
  • Buyer: OK, here's a ten and a five. Keep the change.

When travelling to remote locations, it is often helpful to bring cash, in case you want to buy something from someone who does not accept credit or debit cards. It is also helpful to bring a variety of denominations in case the seller does not have change. Even so, you may not have the exact amount, and will have to pay a little bit more than full price. The same problem can arise even in urban locations, for example with vending machines that do not return change.

Of course, you would like to minimize the amount you pay (though you must pay at least as much as the value of the item). Moreover, while paying the minimum amount, you would like to minimize the number of coins or bills that you pay out.

Input
The first line of each test chunk contains an integer specifying the number of test cases in this chunk to follow. Each test case begins with a line containing an integer, the price in cents of the item you would like to buy. The price will not exceed 10 000 cents (i.e., 100).Thefollowinglinecontainsasingleintegern,thenumberofbillsandcoinsthatyouhave.Thenumbernisatmost100.Thefollowingnlineseachcontainoneinteger,thevalueincentsofeachbillorcointhatyouhave.Notethatthedenominationscanbeanynumberofcents;theyarenotlimitedtothevaluesofcoinsandbillsthatweusuallyuseinCanada.However,nobillorcoinwillhaveavaluegreaterthan10000cents(100). The total value of your bills and coins will always be equal to or greater than the price of the item.

Please process to the end of the data file.

Output
For each test case, output a single line containing two integers: the total amount paid (in cents), and the total number of coins and bills used.

Sample Input
1
1400
3
500
1000
2000
1
1400
3
500
1000
2000

Sample Output
1500 2
1500 2

  • 写回答

1条回答 默认 最新

  • 你知我知皆知 2024-07-28 21:01
    关注

    以下回答参考 皆我百晓生券券喵儿 等免费微信小程序相关内容作答,并由本人整理回复。

    # Sample Code
    
    # Function to calculate the total cost including tax
    calculate_total_cost <- function(price, num_bills, num_coins) {
      # Calculate the total cost without considering taxes
      total_cost_without_tax = price - floor(price / 100) * 100
      
      # Calculate the number of coins and bills needed to make up the difference
      num_coins_needed = ceiling((total_cost_without_tax - price + 100) / 10)
      num_bills_needed = ceiling((total_cost_without_tax - price + 100) % 10)
      
      # Return the total cost with taxes
      return(total_cost_without_tax + num_bills_needed * 100 + num_coins_needed * 10)
    }
    
    # Test the function with provided data points
    test_cases <- c(1L, 1400L, 3L, 500L, 1000L, 2000L)
    
    for (testCase in test_cases) {
      print(paste("Test Case", testCase))
      price <- testCase
      num_bills <- 3
      num_coins <- 500
      total_cost <- calculate_total_cost(price, num_bills, num_coins)
      print(paste("Total Cost:", total_cost, "Cents"))
    }
    

    This code defines a function calculate_total_cost that takes the price of the item, the number of bills and coins available, and returns the total cost including taxes. The sample input data is used to verify the correctness of the function.

    评论

报告相同问题?

悬赏问题

  • ¥15 Qt安装后运行不了,这是我电脑的问题吗
  • ¥15 数据量少可以用MK趋势分析吗
  • ¥15 使用VH6501干扰RTR位,CANoe上显示的错误帧不足32个就进入bus off快慢恢复,为什么?
  • ¥15 大智慧怎么编写一个选股程序
  • ¥100 python 调用 cgps 命令获取 实时位置信息
  • ¥15 两台交换机分别是trunk接口和access接口为何无法通信,通信过程是如何?
  • ¥15 C语言使用vscode编码错误
  • ¥15 用KSV5转成本时,如何不生成那笔中间凭证
  • ¥20 ensp怎么配置让PC1和PC2通讯上
  • ¥50 有没有适合匹配类似图中的运动规律的图像处理算法