Banknote issue - Algorithm

Banknotes decomposition

Problem Description

In this problem, you need to read an integer value and broken down into multiple bills and, each denomination banknotes can use more than one, and asked banknotes used in the smallest possible number.

Please read the output value bank notes and lists.

Banknotes in denominations may 100,50,20,10,5,2,1.

输入格式
Enter an integer N.

输出格式
Referring to the number of output sample requirements, and outputs the read value of banknotes of each denomination.

数据范围
0<N<1000000
输入样例:
576
输出样例:
576
5 nota(s) de R$ 100,00 1 nota(s) de R$ 50,00 1 nota(s) de R$ 20,00 0 nota(s) de R$ 10,00 1 nota(s) de R$ 5,00 0 nota(s) de R$ 2,00 1 nota(s) de R$ 1,00

problem analysis

Split the problem, first of all start from the largest, after selecting the appropriate number, then select the second-largest amount, and so on

Code

#include <iostream>
#include <bits/stdc++.h>
using namespace std;  

int main(){
	//在这个问题中,你需要读取一个整数值并将其分解为多张钞票的和,
	//每种面值的钞票可以使用多张,并要求所用的钞票数量尽可能少。

	//请你输出读取值和钞票清单。

	//钞票的可能面值有100,50,20,10,5,2,1。
	long money,m;
	int yb,ws,es,s,w,e,y;//定义每种金额的个数 
	cin>>money;
	m=money;
	yb=money/100;//100的个数
	money=money%100;//得到新的钱
	ws=money/50;//50的个数
	money=money%50;
	es=money/20;//20的个数
	money=money%20;
	s=money/10;//10的个数
	money=money%10;
	w=money/5;//5的个数
	money=money%5;
	e=money/2;
	money=money%2;
	y=money;
	cout<<m<<endl;
	cout<<yb<<" nota(s) de R$ 100,00"<<endl;
	cout<<ws<<" nota(s) de R$ 50,00"<<endl;
	cout<<es<<" nota(s) de R$ 20,00"<<endl;
	cout<<s<<" nota(s) de R$ 10,00"<<endl;
	cout<<w<<" nota(s) de R$ 5,00"<<endl;
	cout<<e<<" nota(s) de R$ 2,00"<<endl;
	cout<<y<<" nota(s) de R$ 1,00"<<endl;
	return 0;
}

operation result

574
574
5 nota(s) de R$ 100,00
1 nota(s) de R$ 50,00
1 nota(s) de R$ 20,00
0 nota(s) de R$ 10,00
0 nota(s) de R$ 5,00
2 nota(s) de R$ 2,00
0 nota(s) de R$ 1,00

to sum up

Banknotes decomposition problem, in fact, a number of decomposition, it is decomposed to the corresponding digit number was divisible, taking over after the rest of the decomposition

Published 59 original articles · won praise 5 · Views 5061

Guess you like

Origin blog.csdn.net/qq_38496329/article/details/104066626