码迷,mamicode.com
首页 > 编程语言 > 详细

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

时间:2014-11-27 00:21:20      阅读:306      评论:0      收藏:0      [点我收藏+]

标签:des   blog   io   os   sp   on   2014   log   cti   

/*********************************************************************************  
 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;
    }
}

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

标签:des   blog   io   os   sp   on   2014   log   cti   

原文地址:http://blog.csdn.net/drivermonkey/article/details/41526527

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!