问题 G: 找零钱
时间: 1 Sec 内存: 128 MB
献花: 36 解决: 36
[献花][花圈][TK题库]
题目描述
小智去超市买东西,买了不超过一百块的东西。收银员想尽量用少的纸币来找钱。
纸币面额分为50 20 10 5 1 五种。请在知道要找多少钱n给小明的情况下,输出纸币数量最少的方案。 1<=n<=99;
输入
有多组数据 1<=n<=99;
输出
对于每种数量不为0的纸币,输出他们的面值*数量,再加起来输出
样例输入
25
32
样例输出
20*1+5*1
20*1+10*1+1*2
#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <fstream>
#include <string>
#include <cmath>
#include <algorithm>
#include <queue>
using namespace std;
typedef struct
{
int M;
int c;
}relation;
int main()
{
#ifdef _DEBUG
//freopen("data.txt", "r+", stdin);
fstream cin("data.txt");
#endif // _DEBUG
int M, LM[] = { 50,20,10,5,1 };
queue<relation> que; relation tmp;
while (cin >> M)
{
int i = 0;
while (M > 0)
{
int n = 0;
while (M - LM[i] >= 0)
{
++n;
M -= LM[i];
}
if (n)
{
tmp.M = LM[i]; tmp.c = n;
que.push(tmp);
}
++i;
}
while (!que.empty())
{
tmp = que.front();
que.pop();
cout << tmp.M << "*" << tmp.c;
if (!que.empty())
cout << "+";
}
cout << endl;
}
#ifdef _DEBUG
cin.close();
#ifndef _CODEBLOCKS
system("pause");
#endif // !_CODEBLOCKS
#endif // _DEBUG
return 0;
}
/**************************************************************
Problem: 5038
User: Sharwen
Language: C++
Result: 升仙
Time:0 ms
Memory:1712 kb
****************************************************************/
因篇幅问题不能全部显示,请点此查看更多更全内容
Copyright © 2019- awee.cn 版权所有 湘ICP备2023022495号-5
违法及侵权请联系:TEL:199 1889 7713 E-MAIL:2724546146@qq.com
本站由北京市万商天勤律师事务所王兴未律师提供法律服务