一种新的找零钱算法,俺自创的,请大虾批评指正
人民币有100,50,20 ,10,5,1,0.5,0.1, 0.05,0.02,0.01几种币值,现在属于一个币值,比如185.56元,求怎样分解为零钱,使所有零钱张数最少,比如255.55元的最少张数就是100元2张,50元一张,5元一张,5毛一张,5分一张,这样就是钱币数最少的分解,下面是代码,在VS2010中调试通过
#include "stdafx.h"
#include "stdio.h"
#include "stdlib.h"
#include <iostream>
#include <vector>
#include <string>
using namespace std;
vector<int> result_ZS;//整数部分分解为多少种币值
vector<int> result_XS;//小数部分分解为多少种币值
void exchange_ZhengShu(string strZS);
void exchange_XiaoShu(string strXS);
void print_ZS();
void print_XS();
int input()
{
string str1="",strZhengShu="",strXiaoShu="",cursor="";
cout<<"开始,请输入钱数:"<<endl;
fflush(stdin);
getline(cin,str1,'\n');
cursor+=str1;
if(cursor==""||str1=="")
{
printf("输入不能为空值\n");
system("pause");
return 0;
}
string str2=".";
int mark=0,bitnum=0;
int pos=cursor.find(str2);
if(pos==-1)//不含小数点
{
mark=0;
}
if(pos!=-1)//含有小数点
{
mark=1;
}
//cout<<"pos: "<<pos<<endl;
//cout<<"mark: "<<mark<<endl;
if(mark==0)//不含小数点
{
cursor+=".00";
string in1="",in2="";
in1+=cursor;
in2+=cursor;
exchange_ZhengShu(in1);
exchange_XiaoShu(in2);
print_ZS();
print_XS();
system("pause");
return 0;
}
if(mark==1)//含有小数点
{
if(cursor[0]=='.')//如果首位是小数点
{
string aaa="";
aaa+=cursor;
cursor="";
cursor+="0";
cursor+=aaa;
aaa="";
}
int mmm=cursor.length();
if(cursor[mmm-1]=='.')//如果末位是小数点
{
//cout<<"+00之前: "<<cursor<<endl;
cursor+="00";
//cout<<"+00之后: "<<cursor<<endl;
}
if(cursor[mmm-2]=='.')//如果倒数第二位是小数点
{
//cout<<"+0之前: "<<cursor<<endl;
cursor+="0";
//cout<<"+0之后: "<<cursor<<endl;
}
//printf("length: %d\n",cursor.length());
string in1="",in2="";
in1+=cursor;
in2+=cursor;
exchange_ZhengShu(in1);
exchange_XiaoShu(in2);
print_ZS();
print_XS();
return 0;
}
}
void exchange_ZhengShu(string strZS)
{
string innerStr="";
vector<int> bizhi;//共有多少种币值
while(strZS[0]!='.')
{
innerStr+=strZS[0];
strZS.erase(0,1);
}
bizhi.push_back(100);
bizhi.push_back(50);
bizhi.push_back(20);
bizhi.push_back(10);
bizhi.push_back(5);
bizhi.push_back(1);
int ZhengShu=atoi(innerStr.c_str());//整数部分钱数
int i=0;
while(ZhengShu>0)
{
if(ZhengShu>=bizhi[i])
{
int cursor=bizhi[i];
result_ZS.push_back(cursor);
ZhengShu=ZhengShu-cursor;
cursor=0;
}
if(ZhengShu<bizhi[i]&&ZhengShu>0)
{
i++;
}
}
}
void exchange_XiaoShu(string strXS)
{
vector<int> bizhi;//共有多少种币值
string innerStr="";
bizhi.push_back(50);
bizhi.push_back(10);
bizhi.push_back(5);
bizhi.push_back(2);
bizhi.push_back(1);
while(strXS[0]!='.')
{
strXS.erase(0,1);
}
strXS.erase(0,1);
for(int b=0;b<=1;b++)
{
innerStr+=strXS[0];
strXS.erase(0,1);
}
int XiaoShu=atoi(innerStr.c_str());//小数部分钱数
int i=0;
while(XiaoShu>0)
{
if(XiaoShu>=bizhi[i])
{
int cursor=bizhi[i];
result_XS.push_back(cursor);
XiaoShu=XiaoShu-cursor;
cursor=0;
}
if(XiaoShu<bizhi[i]&&XiaoShu>0)
{
i++;
}
}
}
void print_ZS()
{
int zhibizhangshu=result_ZS.size();
zhibizhangshu+=result_XS.size();
printf("共有钱币张数:%d\n",zhibizhangshu);
for(int a=0;a<result_ZS.size();a++)
{
printf("币值: %d\n",result_ZS[a]);
}
}
void print_XS()
{
vector<float> result_XS_Float;
for(int b=0;b<result_XS.size();b++)
{
if(result_XS[b]==50)
{
result_XS_Float.push_back(0.5);
}
if(result_XS[b]==10)
{
result_XS_Float.push_back(0.1);
}
if(result_XS[b]==5)
{
result_XS_Float.push_back(0.05);
}
if(result_XS[b]==2)
{
result_XS_Float.push_back(0.02);
}
if(result_XS[b]==1)
{
result_XS_Float.push_back(0.01);
}
}
for(int a=0;a<result_XS_Float.size();a++)
{
printf("币值: %.2f\n",result_XS_Float[a]);
}
}
int main()
{
input();
system("pause");
}