首页 > 代码库 > Change maker problem-找零(1-99美分)
Change maker problem-找零(1-99美分)
Change maker problem-找零(1-99美分),允许重复计算:
//Change maker problem-找零(1-99美分) #include<iostream> using namespace std; void compute_coins(int coin_value,int& number,int& amount_left); int main() { int cents,number[2],amount_left; char ans; do{ cout<<"Enter the (1-99)cents:\n"; cin>>cents; number[0]=number[1]=number[2] = {0}; amount_left = cents; while(amount_left > 0) { if(amount_left >= 25) { compute_coins(25,number[0],amount_left); compute_coins(10,number[1],amount_left); compute_coins(1,number[2],amount_left); } else if(amount_left >= 10 && amount_left < 25) { compute_coins(10,number[1],amount_left); compute_coins(1,number[2],amount_left); } else if(amount_left >= 1 && amount_left < 10) compute_coins(1,number[2],amount_left); } cout<<cents<<" cents can be given as "<<endl <<number[0]<<" quarter(s) "<<number[1]<<" dime(s) and "<<number[2]<<" penny(pennies)"<<endl; cout<<"Do you want again?"; cin>>ans; }while(‘y‘ == ans || ‘Y‘ == ans); } void compute_coins(int coin_value,int& number,int& amount_left) { switch(coin_value) { case 25: number = amount_left/25; amount_left = amount_left % coin_value; break; case 10: number = amount_left/10; amount_left = amount_left % coin_value; break; case 1: number = amount_left/1; amount_left = amount_left % coin_value; break; default: cout<<"ERROR!"; } }
结果:
Enter the (1-99)cents: 86 86 cents can be given as 3 quarter(s) 1 dime(s) and 1 penny(pennies) Do you want again?y Enter the (1-99)cents: 25 25 cents can be given as 1 quarter(s) 0 dime(s) and 0 penny(pennies) Do you want again?y Enter the (1-99)cents: 10 10 cents can be given as 0 quarter(s) 1 dime(s) and 0 penny(pennies) Do you want again?y Enter the (1-99)cents: 1 1 cents can be given as 0 quarter(s) 0 dime(s) and 1 penny(pennies) Do you want again?
Change maker problem-找零(1-99美分)
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。