注册 登录
编程论坛 C++教室

c++找零问题

huangyu0 发布于 2009-10-02 18:51, 2829 次点击
用c++编写找零钱问题。。
找回的张数要最少 ;eg:东东45元  你给100 那么找回就一定要是一张50 一张5 不能是2张20 一张10一张5....
 我写出来的感觉总不打对啊。。
 各位有什么方法说说。。给个参考。
10 回复
#2
flyingcloude2009-10-02 21:46
你只要从最大面值的开始减,那么肯定不会错了
比如你的例子中:
        你程序安以下思路实现:
      100-45=55;
    55<100
    next:55>50
    next:55 - 50 =5;
    next:5>50;
    next:5>20;
    next:5>10;
    next:5=5
    next : 5 - 5 =0;

比如你东西是55元,给了100;那么
    100-55 = 45;
    45<100;
    next : 45<50;
    next :45 >20;
    next :45 -20 = 25;
    next :25>20
    next :25-20 =5;
    next : 5<20;
    next : 5<10;
    next :5 =5;
    next :5-5=0

按照这样的过程肯定找回的张数是最少的
#3
yxb00012009-10-03 00:22
这题是"a%b"求余问题,先求大金额的余数,再求次大金额的余数......理论上一直可求至1分止,实际上可根据具体情况定。
#4
huangyu02009-10-04 00:38
楼上的大大能给些代码么?。
你的意思是按递归做还是?。
我想了一些感觉写不出来啊。
能给小部分源代码么?。
或者是给个说明 改用几个部分完成这个程序、
#5
narcissushtl2009-10-04 08:39
int Method_RMB(double change_money, double change_kind[], int num[], int i) //change_money应该找零的钱的总数;两个数组须初始化赋0
{
    double a=(double)((int)(100*change_money)%(int)(100*denomination_RMB[i])); //denomination_RMB[]以人民币面值为元素的数组
        if(a!=0){            
            change_kind[i]=denomination_RMB[i];
            num[i]=(int)(change_money/denomination_RMB[i]);
            i++;
            Method_RMB(a/100,change_kind,num,i);
        }
        else{
            change_kind[i]=denomination_RMB[i];
            num[i]=(int)(change_money/denomination_RMB[i]);
            return 1;
        }
}
//求出符合条件的找零方法:面值存入数组change_kind,张数存入数组num。最后只要将两个数组中非零元素打印出来就可以了
#6
huangyu02009-10-04 18:15
楼上的看不懂啊。。。请二楼的大虾帮忙写个简单点的、、?谢谢啦。
#7
huangyu02009-10-04 20:19

#include <iostream>  
 
 
using namespace std;  
 
void main(){  
float m,n;  
float a; //a应该为float,否则一减就只剩下整数了  
int dollar,cent1,cent2,cent3,cent4,cent5,cent6,cent7,cent8;  
cout<<"################################################################################";
cout<<"                                  欢迎使用本程序                                ";
cout<<"                               本程序主要由%何/完成                             ";
cout<<"                                                                                ";
cout<<"                                                             By SharpshootER    ";
cout<<"################################################################################";
cout<<endl;
cout<<"################################################################################";
cout<<"            本程序可使用的金额为100 50 20 10 5 1 0.5 0.1 单位-元;              ";
cout<<"                                                                                ";
cout<<"################################################################################";
cout<<endl;
cout<<"请输入应收的金额:";  
cin>>m;
cout<<endl;
L1:cout<<"请输入实收的金额:";  
cin>>n;
cout<<endl;  
while(n<m){  
cout<<"实收金额不足!"<<endl;  
goto L1; goto L2;}  
L2:a=n-m;  
dollar=a*10;
cent1=dollar/1000;
cent2=(cent1%1000)/500;
cent3=((cent1%1000)%500)/200;
cent4=(((cent1%1000)%500)%200)/100;
cent5=((((cent1%1000)%500)%200)%100)/50;
cent6=(((((cent1%1000)%500)%200)%100)%50)/10;
cent7=((((((cent1%1000)%500)%200)%100)%50)%10)/5;
cent8=(((((((cent1%1000)%500)%200)%100)%50)%10)%5)/1;
cout<<"找零:"<<endl<<endl;
cout<<"100元:"<<cent1<<"张."<<endl;
cout<<"50元:"<<cent2<<"张."<<endl;
cout<<"20元:"<<cent3<<"张."<<endl;
cout<<"10元:"<<cent4<<"张."<<endl;
cout<<"5元:"<<cent5<<"张."<<endl;
cout<<"1元:"<<cent6<<"张."<<endl;
cout<<"0.5元:"<<cent7<<"张."<<endl;
cout<<"0.1元:"<<cent8<<"张."<<endl;
cout<<endl;
system("pause");
}
 
 
请教:
我这样写有什么错么?
为什么编译出来的结果全部为0啊?
我感觉没写错啊。
#8
narcissushtl2009-10-04 21:26
cent2=(cent1%1000)/500;  
cent3=((cent1%1000)%500)/200;  
cent4=(((cent1%1000)%500)%200)/100;  
cent5=((((cent1%1000)%500)%200)%100)/50;  
cent6=(((((cent1%1000)%500)%200)%100)%50)/10;  
cent7=((((((cent1%1000)%500)%200)%100)%50)%10)/5;  
cent8=(((((((cent1%1000)%500)%200)%100)%50)%10)%5)/1;  

把这里的cent1全改成dollar。。。你这样子不是0才怪。。。
#9
huangyu02009-10-04 21:31
谢谢。。。我真是菜的美化说。。楼上的谢谢蜡
#10
flyingcloude2009-10-05 17:26
以下是引用huangyu0在2009-10-4 18:15:55的发言:

楼上的看不懂啊。。。请二楼的大虾帮忙写个简单点的、、?谢谢啦。
  应你要求写了个

 #include<iostream>
 #include<vector>
 using namespace std;

int main()
{
    int a[5]={50,20,10,5,1}; //可以用来找零的钱,以元为单位,不考虑角分

    int money,sum,;
    cout<<"请输入两个大于零的整数,分别表示所付的钱和东西的总价:";
    cin>>money,sum;

    money = money-sum;
    vector<int> v; //存储找回来的钱,需要#include<vector>

    int i=0;
    while(money>0)
    {
        if(money>=a[i])
        {
            v.push_back(a[i]);
            money-=a[i];
        }
        else
            i++;
    }
   
    cout<<"找回来的零钱:"<<'\n';
    for(i=0;i<v.size();i++)
        cout<<v[i]<<" ";
}


[ 本帖最后由 flyingcloude 于 2009-10-5 17:39 编辑 ]
#11
haitao99992009-10-05 20:45
我也写了个,如下:
#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();
     
}
   
1