递归练习之换零钱方式统计(c/c++)

/*********************************************************************************  
 Copyright (C), 1988-1999, drvivermonkey. Co., Ltd.  
 File name:   
 Author: Driver Monkey  
 Version:   
 Mail:bookworepeng@hotmail.com  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;
    }
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值