当前位置:网站首页>OpenJudge NOI 1.13 13:人民币支付
OpenJudge NOI 1.13 13:人民币支付
2022-06-10 04:33:00 【君义_noip】
【题目链接】
【题目考点】
1. 整除运算
2. 计数数组
【解题思路】
该题用的是贪心思想,先尽量取大面值的货币来凑钱,如果添加一张货币后,货币总额大于要凑的钱数,那么选择小一号的货币来凑钱。
将面值从大到小存储在数组a中。
解法1:使用整除运算
遍历数组a,当前需要凑的钱数为n,关注的面值为a[i],使用该面值货币的数量为n/a[i],剩余钱数为n%a[i]。
让n变为剩余的钱数,再看下一个面值更小的货币。
解法2:使用计数数组
当前关注的面值为a[i], 每次取用一个该面值的货币,直到取用该面值货币后,剩下的钱数小于0,那么再看下一个面值更小的货币。用计数数组记录每种货币使用的个数。
【题解代码】
解法1:使用整除运算
#include<bits/stdc++.h>
using namespace std;
int main()
{
int n, a[6] = {
100, 50, 20, 10, 5, 1};
cin >> n;
for(int i = 0; i < 6; ++i)
{
cout << n/a[i] << endl;
n %= a[i];
}
return 0;
}
解法2:使用计数数组
#include<bits/stdc++.h>
using namespace std;
int main()
{
int n, i = 0, c[6] = {
}, a[6] = {
100, 50, 20, 10, 5, 1};
cin >> n;
while(n > 0)
{
if(n >= a[i])//如果剩下要凑的钱大于等于当前关注的面值
{
c[i]++;//该面值货币使用数量加1
n -= a[i];
}
else
i++;
}
for(i = 0; i < 6; ++i)//输出每种货币使用的个数
cout << c[i] << endl;
return 0;
}
边栏推荐
- Exemple de démarrage JDBC
- QT cannot connect to local MySQL
- Basic methods of stack and related problems
- .NET C#基础(7):接口 - 人如何和猫互动
- Golang learning 4: closures and defers
- Gevent | use it asynchronously!
- tensorflow 中的 cross_entropy
- When to use @componentscan? What is the difference with @mapperscan?
- [laser principle and application-1]: what is a laser and its common applications
- [in depth study of 4g/5g/6g topic -29]: 5g NR startup process 5.1 - NR network architecture, core network configuration of base stations
猜你喜欢

Pysimplegui classic practice: how to read this Chinese character?

Fastapi-16-page beautification-1

JMeter testing TCP million connections

Fastapi-14-file upload-2

ThreadLocal basic and advanced use

CSDN audio and video technology developers' online survey

Execution strategy of application software efficiency test

Gevent | 异步就用它!

Super easy to use chrome plug-in!

mindspore1.6conda安装gpu版本验证失败
随机推荐
Puzzling color deviation of unity illumination black
Storage engine of MySQL database
大事件回顾 | Eolink 5月重要动态速览!
测试工程师提高质量的OKR该如何写?
[learn FPGA programming from scratch -13]: quick start chapter - operation step 3 (functional simulation) -2-mentor HDL simulation tool introduction to Modelsim tool and basic principle of functional
Who ate IO?
CSDN audio and video technology developers' online survey
Yyds dry goods inventory solution sword finger offer: rectangular coverage
[understanding of opportunity -22]: Guiguzi - the art of closing Tibet. Collect your talents in time to protect yourself in the workplace, business and business activities
多商户商城小程序源码有何优势?
[graduation project 2] intelligent range hood system based on stm32
Celery | 任务队列神器
ThreadLocal basic and advanced use
超好用的 Chrome 插件!
23 very useful shell scripts
Good news 𞓜 wangchain technology signed the Miluo cultural, tourism and sports industry project to create a digital village on the "chain"
[in depth study of 4g/5g/6g topic -29]: 5g NR startup process 5.1 - NR network architecture, core network configuration of base stations
Distributed data object: HyperTerminal 'global variable'
golang学习之四:闭包、defer
[in depth study of 4g/5g/6g topic -28]: 5g NR startup process 4.6 - msg5 (rrcsetupcomplete) message scheduling