我也写了个,如下:
#include <iostream>
using namespace std;
int convert(){
int value,pay;
while(1){
cout<<"请按先后顺序输入需支付金额和实际支付金额,中间空格隔开,然后回车:"<<endl;
cin>>value>>pay;
if(value<0)
{
cout<<"支付钱数为负值!"<<endl;
continue;
}
if(value>pay)
{
cout<<"你给的钱数不够,请补充支付:"<<endl;
continue;
}
int nMargin,n100,n50,n20,n10,n5,n2,n1;
nMargin = pay-value;
n100=nMargin/100;
n50=nMargin%100/50;
n20=nMargin%100%50/20;
n10=nMargin%100%50%20/10;
n5=nMargin%100%50%20%10/5;
n2=nMargin%100%50%20%10%5/2;
n1=nMargin%100%50%20%10%5%2/1;
cout<<"应找金额为:"<<endl;
cout<<"100元:"<<n100<<"张"<<endl;
cout<<" 50元: "<<n50<<"张"<<endl;
cout<<" 20元: "<<n20<<"张"<<endl;
cout<<" 10元:"<<n10<<"张"<<endl;
cout<<" 5元: "<<n5<<"张"<<endl;
cout<<" 2元: "<<n2<<"张"<<endl;
cout<<" 1元: "<<n1<<"张"<<endl;
break;
}
return 0;
}
int main(){
cout<<"*********************************************************"<<endl;
cout<<"*********程序实现找钱功能,找回的钞票张数最少化!********"<<endl;
cout<<"***程序支持钞票面额最大到佰元,最小到元。如需扩充功能****"<<endl;
cout<<"*********改变参数类型为float型,增加变量即可*************"<<endl;
cout<<"*******************复杂度变化不大************************"<<endl;
cout<<" "<<endl;
cout<<" "<<endl;
cout<<" by haitao9999. "<<endl;
convert();
}
#include <iostream>
using namespace std;
int convert(){
int value,pay;
while(1){
cout<<"请按先后顺序输入需支付金额和实际支付金额,中间空格隔开,然后回车:"<<endl;
cin>>value>>pay;
if(value<0)
{
cout<<"支付钱数为负值!"<<endl;
continue;
}
if(value>pay)
{
cout<<"你给的钱数不够,请补充支付:"<<endl;
continue;
}
int nMargin,n100,n50,n20,n10,n5,n2,n1;
nMargin = pay-value;
n100=nMargin/100;
n50=nMargin%100/50;
n20=nMargin%100%50/20;
n10=nMargin%100%50%20/10;
n5=nMargin%100%50%20%10/5;
n2=nMargin%100%50%20%10%5/2;
n1=nMargin%100%50%20%10%5%2/1;
cout<<"应找金额为:"<<endl;
cout<<"100元:"<<n100<<"张"<<endl;
cout<<" 50元: "<<n50<<"张"<<endl;
cout<<" 20元: "<<n20<<"张"<<endl;
cout<<" 10元:"<<n10<<"张"<<endl;
cout<<" 5元: "<<n5<<"张"<<endl;
cout<<" 2元: "<<n2<<"张"<<endl;
cout<<" 1元: "<<n1<<"张"<<endl;
break;
}
return 0;
}
int main(){
cout<<"*********************************************************"<<endl;
cout<<"*********程序实现找钱功能,找回的钞票张数最少化!********"<<endl;
cout<<"***程序支持钞票面额最大到佰元,最小到元。如需扩充功能****"<<endl;
cout<<"*********改变参数类型为float型,增加变量即可*************"<<endl;
cout<<"*******************复杂度变化不大************************"<<endl;
cout<<" "<<endl;
cout<<" "<<endl;
cout<<" by haitao9999. "<<endl;
convert();
}