/********************************************************************************* Copyright (C), 1988-1999, drvivermonkey. Co., Ltd. File name: Author: Driver Monkey Version: Mail:[email protected] qq:196568501 Date: 2014.04.02 Description: 递归练习之换零钱方式统计(c/c++) *********************************************************************************/ #include <iostream> #include <sstream> #include <fstream> #include <iostream> #include <iomanip> #include <string> #include <memory.h> #include <thread> #include <stdlib.h> /* labs */ #include <math.h> using namespace std; typedef struct { int value; int flag; }valut_t; typedef struct { valut_t value_50; valut_t value_25; valut_t value_10; valut_t value_5; valut_t value_1; }all_value_t; static int fuction(int total_money, all_value_t value); static int is_only_50_change(all_value_t& value); static int is_only_25_change(all_value_t& value); static int is_only_10_change(all_value_t& value); static int is_only_5_change(all_value_t& value); static int is_only_1_change(all_value_t& value); int main() { all_value_t value = {{50,1},{25,1},{10,1},{5,1},{1,1}}; cout<<"fuction = " <<fuction(100,value)<<endl; return 0; } static int fuction(int total_money, all_value_t value) { if(total_money == 0) { return 1; }else if(total_money < 0) { return 0; } if(value.value_50.flag == true) { if(is_only_50_change(value)) { return 1; } all_value_t temp_value = value; temp_value.value_50.flag = false; return fuction(total_money, temp_value) + fuction(total_money -50 , value); } else if(value.value_25.flag== true) { if(is_only_25_change(value)) { return 1; } all_value_t temp_value = value; temp_value.value_25.flag = false; return fuction(total_money, temp_value) + fuction(total_money -25 , value); }else if(value.value_10.flag == true) { if(is_only_10_change(value)) { return 1; } all_value_t temp_value = value; temp_value.value_10.flag = false; return fuction(total_money, temp_value) + fuction(total_money -10 , value); }else if(value.value_5.flag == true) { if(is_only_5_change(value)) { return 1; } all_value_t temp_value = value; temp_value.value_5.flag = false; return fuction(total_money, temp_value) + fuction(total_money -5 , value); }if(value.value_1.flag == true) { if(is_only_1_change(value)) { return 1; } all_value_t temp_value = value; temp_value.value_1.flag = false; return fuction(total_money, temp_value) + fuction(total_money -1 , value); }else { return 0; } } static int is_only_50_change(all_value_t& value) { if((value.value_25.flag & value.value_10.flag & value.value_5.flag & value.value_1.flag) == false) { return true; }else { return false; } } static int is_only_25_change(all_value_t& value) { if(((value.value_50.flag == false) &&( value.value_10.flag == false) &&( value.value_5.flag == false) &&( value.value_1.flag == false)) && (value.value_25.flag == true)) { return true; }else { return false; } } static int is_only_10_change(all_value_t& value) { if(((value.value_25.flag == false) &&( value.value_50.flag == false) &&( value.value_5.flag == false) &&( value.value_1.flag == false)) && (value.value_10.flag == true)) { return true; }else { return false; } } static int is_only_5_change(all_value_t& value) { if(((value.value_25.flag == false) &&( value.value_10.flag == false) &&( value.value_50.flag == false) &&( value.value_1.flag == false)) && (value.value_5.flag == true)) { return true; }else { return false; } } static int is_only_1_change(all_value_t& value) { if(((value.value_25.flag == false) &&( value.value_10.flag == false) &&( value.value_5.flag == false) &&( value.value_50.flag == false)) && (value.value_1.flag == true)) { return true; }else { return false; } }
时间: 2024-12-26 11:04:40