Find all possible combinations of k positive numbers that add up to a number n,each combination should be a unique set of numbers.

 /**
* Return an array of arrays of size *returnSize.
* The sizes of the arrays are returned as *columnSizes array.
* Note: Both returned array and *columnSizes array must be malloced, assume caller calls free().
*/
#define SIZE 600
int** function(int k, int n, int** columnSizes, int* returnSize) {
int **ret=(int**)malloc(sizeof(int*)*SIZE);
int *sum=(int*)calloc(SIZE,sizeof(int));
int *countArray=(int*)calloc(SIZE,sizeof(int));
int temp_n=n;
int count=;
int temp=;
int temp_k=;
int back=;
*returnSize=;
if(k==){
*returnSize=;
columnSizes[]=(int*)malloc(sizeof(int));
columnSizes[][]=;
ret[]=(int*)malloc(sizeof(int));
ret[][]=n;
return ret;
}
for(int i=;i<SIZE;i++){
ret[i]=(int*)calloc(k,sizeof(int));
}
while(temp*k+(k-)*k/<=n){
ret[(*returnSize)][]=temp;
countArray[(*returnSize)]++;
sum[(*returnSize)]=temp;
temp++;
(*returnSize)++;
}
while(ret[count][]!=){
temp=ret[count][countArray[count]-]+;
temp_k=k-countArray[count];
while(temp*temp_k+(temp_k-)*temp_k/<=(n- sum[count])){
if(temp_k==){
ret[count][countArray[count]]=n- sum[count];
break;
}
ret[count][countArray[count]]=temp;
back=sum[count];
sum[count]=sum[count]+temp;
countArray[count]++;
temp++;
while(temp*temp_k+(temp_k-)*temp_k/<=(n- sum[count])){
for(int i=;i<countArray[count]-;i++){
ret[(*returnSize)][i]=ret[count][i];
}
ret[(*returnSize)][countArray[count]-]=temp;
countArray[(*returnSize)]=countArray[count];
sum[(*returnSize)]=back+temp;
temp++;
(*returnSize)++;
}
temp=ret[count][countArray[count]-]+;
temp_k=k-countArray[count];
}
count++;
}
columnSizes[]=(int*)malloc(sizeof(int)*(*returnSize));
for(int i=;i<(*returnSize);i++){
columnSizes[][i]=k;
}
return ret;
}

找出k个数相加得n的所有组合的更多相关文章

  1. 用JAVA写一个函数,功能例如以下: 随意给定一组数, 找出随意数相加之后的结果为35(随意设定)的情况

    用JAVA写一个函数.功能例如以下:随意给定一组数,比如{12,60,-8,99,15,35,17,18},找出随意数相加之后的结果为35(随意设定)的情况. 能够递归算法来解: package te ...

  2. 找出数组中出现次数超过一半的数,现在有一个数组,已知一个数出现的次数超过了一半,请用O(n)的复杂度的算法找出这个数

    找出数组中出现次数超过一半的数,现在有一个数组,已知一个数出现的次数超过了一半,请用O(n)的复杂度的算法找出这个数 #include<iostream>using namespace s ...

  3. search for a range(找出一个数在数组中开始和结束位置)

    Given an array of integers sorted in ascending order, find the starting and ending position of a giv ...

  4. 找出N个数中最小的k个数问题(复杂度O(N*logk))

    这是一个经典的算法题,下面给出的算法都在给定的数组基础上进行,好处时不用分配新的空间,坏处是会破坏原有的数组,可以自己分配新的空间以避免对原有数组的破坏. 思路一 先直接排序,再取排序后数据的前k个数 ...

  5. java中请给出例子程序:找出两个数的最大公约数和最小公倍数

    9.2 找出12和8的最大公约数和最小公倍数.     public class Test {     public static void main(String[] args) {         ...

  6. 【算法学习笔记】Meissel-Lehmer 算法 (亚线性时间找出素数个数)

    「Meissel-Lehmer 算法」是一种能在亚线性时间复杂度内求出 \(1\sim n\) 内质数个数的一种算法. 在看素数相关论文时发现了这个算法,论文链接:Here. 算法的细节来自 OI w ...

  7. 找出n个数中出现了奇数次的两个数

    如果是找只出现了奇数次的一个数, 那么我们从头异或一遍就可以. 那么如何找出现了奇数次的两个数呢? 首先我们还是从头异或一遍, 然后结果肯定不为0, 对于异或出来的结果, 如果这个数的某一位是1, 说 ...

  8. 3sum(从数组中找出三个数的和为0)

    Given an array S of n integers, are there elements a, b, c in S such that a + b + c = 0? Find all un ...

  9. 已知一个数出现的次数超过了一半,请用O(n)的复杂度的算法找出这个数

    #include<iostream> using namespace std; //#define maxn 2000010 #include<stdio.h> //int a ...

随机推荐

  1. linux开放80 端口

    1.使用su登录管理员用户 2.编辑防火墙配置文件 vim /etc/sysconfig/iptables 3.在里面加入后保存 #open port 80 -A INPUT -p TCP -m st ...

  2. 解决mac 中的myeclipse控制台中文乱码问题

    http://www.myexception.cn/eclipse/1742588.html 解决 http://my.oschina.net/u/555006/blog/195013

  3. 判断json是否包含了每个键 PHP

    (1)可以用array_key_exists去判断object对象或array数组中是否含有某个键: (2)不可以用isset去判断判断object对象或array数组中是否含有某个键 $decode ...

  4. UVA - 12333 字典树+大数

    思路:用字典树将前40个数字记录下来,模拟大数加法即可. AC代码 #include <cstdio> #include <cmath> #include <algori ...

  5. NOIP2017 - 宝藏

    LibreOJ链接 Description 给出一个\(n(n\leq12)\)个点\(m(m\leq1000)\)条边的带权无向图,求该图的一棵生成树,使得其边权×该边距根的深度之和最小. Solu ...

  6. 在linux系统中

    A .etc下放置配置文件 B./var下放置日志文件 C./root超级用户主目录 D./home 使用者家目录 /bin  二进制执行文件,也就是命令文件 /etc 下存放的是配置文件 /dev ...

  7. AES对称加密

    import javax.crypto.Cipher; import javax.crypto.spec.IvParameterSpec; import javax.crypto.spec.Secre ...

  8. 经典案例之MouseJack

    引言:在昨天的文章<无线键鼠监听与劫持>中,我们提到今天会向您介绍一个无线键鼠的监听与劫持的经典案例,<MouseJack>:MouseJack能利用无线鼠标和键盘存在的一些问 ...

  9. 3.3.3 PCI设备对可Cache的存储器空间进行DMA读写

    PCI设备向"可Cache的存储器空间"进行读操作的过程相对简单.对于x86处理器或者PowerPC处理器,如果访问的数据在Cache中命中,CPU会通知FSB总线,PCI设备所访 ...

  10. R语言︱list用法、批量读取、写出数据时的用法

    列表是一种特别的对象集合,它的元素也由序号(下标)区分,但是各元素的类型可 以是任意对象,不同元素不必是同一类型.元素本身允许是其它复杂数据类型,比如,列表 的一个元素也允许是列表.例如: > ...