列出某数所有的加法式
来源:互联网 发布:oracle数据库设计文档 编辑:程序博客网 时间:2024/04/29 01:14
- // ***************************************************************
- // Addition version: 1.0 · date: 12/30/2008
- // -------------------------------------------------------------
- // Author: ZhangLiang
- // -------------------------------------------------------------
- // Copyright (C) 2008 - All Rights Reserved
- // ***************************************************************
- //
- // ***************************************************************
- #include "stdafx.h"
- //////////////////////////////////////////////////////////////////////////
- //Get the min value
- inline int MinValue( int val1,int val2)
- {
- return val1<val2? val1:val2;
- }
- //////////////////////////////////////////////////////////////////////////
- //Output a result
- int OutPut(list<int>& addition)
- {
- for (list<int>::iterator it = addition.begin();it!=addition.end();it++)
- {
- cout<<*it<<" ";
- }
- cout<<"/n";
- return 0;
- }
- //////////////////////////////////////////////////////////////////////////
- //list all result
- //sum: the operate value
- //addition: the container of addend
- //maxAddend: the max addend
- int Addition(int sum,list<int>& addition,int maxAddend)
- {
- if (0== sum)
- {
- OutPut(addition);
- return 0;
- }
- for (int i = 1; i<=maxAddend;i++)
- {
- addition.push_back(i);
- Addition(sum-i,addition,MinValue(sum-i,i));
- addition.pop_back();
- }
- return 0;
- }
- //////////////////////////////////////////////////////////////////////////
- //main function
- int _tmain(int argc, _TCHAR* argv[])
- {
- list<int> lstAddition;//the container of addend
- int nSum = 10; // the operate value
- Addition(nSum,lstAddition,nSum-1);
- system("pause");
- return 0;
- }
Addition函数是实现主要的功能函数,代码就几行而已,很简单。只是用到了递归。
- 列出某数所有的加法式
- 列出长度为n的数组中,所有加和为m的数
- iOS 列出文件加下特定格式的所有文件
- 埃拉托斯特尼筛法--列出小于给定数的所有素数的算法
- 列出所有的索引
- 列出某个目录下的所有文件和列出某目录下所有的子目录
- linux列出所有的服务
- 列出字符串的所有排列
- JAVA列出某文件夹下的所有文件
- java 列出某文件夹下的所有文件
- 递归列出某目录的所有子目录和文件
- 给定一个数N,要求列出所有不大于N的素数
- C# 计算排列组合数,及列出所有组合形式的算法
- 递归打印一个数所有加和的方式
- 列出本机所有的Ip
- 列出数据库中所有的表名
- 列出所有已安装的perl模块
- 列出某个目录下的所有文件
- django中ThreadLocals获取当前用户有问题
- 网易train
- 软件工程在DCS中的应用
- 博客blog类型
- CButtonST类使用说明文档
- 列出某数所有的加法式
- iH如何使用OPC Collector
- 用php获取客户端IP地址的方法,具体代码如下:
- ASP.NET四种页面导航方式的比较与选择
- split命令分割分区的方法
- 写在开博之初
- GE FANUC Proficy部分软件的关系
- VS2005下组件ReportView使用时的一个问题
- 一步一步学Linq to sql(六):探究特性