标题:c++找零问题
只看楼主
haitao9999
Rank: 2
等 级:论坛游民
帖 子:30
专家分:39
注 册:2006-4-10
得分:2 
我也写了个,如下:
#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();
     
}
   
2009-10-05 20:45



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-287465-1-1.html




关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.545269 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved