找出k个数相加得n的所有组合
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的所有组合的更多相关文章
- 用JAVA写一个函数,功能例如以下: 随意给定一组数, 找出随意数相加之后的结果为35(随意设定)的情况
用JAVA写一个函数.功能例如以下:随意给定一组数,比如{12,60,-8,99,15,35,17,18},找出随意数相加之后的结果为35(随意设定)的情况. 能够递归算法来解: package te ...
- 找出数组中出现次数超过一半的数,现在有一个数组,已知一个数出现的次数超过了一半,请用O(n)的复杂度的算法找出这个数
找出数组中出现次数超过一半的数,现在有一个数组,已知一个数出现的次数超过了一半,请用O(n)的复杂度的算法找出这个数 #include<iostream>using namespace s ...
- search for a range(找出一个数在数组中开始和结束位置)
Given an array of integers sorted in ascending order, find the starting and ending position of a giv ...
- 找出N个数中最小的k个数问题(复杂度O(N*logk))
这是一个经典的算法题,下面给出的算法都在给定的数组基础上进行,好处时不用分配新的空间,坏处是会破坏原有的数组,可以自己分配新的空间以避免对原有数组的破坏. 思路一 先直接排序,再取排序后数据的前k个数 ...
- java中请给出例子程序:找出两个数的最大公约数和最小公倍数
9.2 找出12和8的最大公约数和最小公倍数. public class Test { public static void main(String[] args) { ...
- 【算法学习笔记】Meissel-Lehmer 算法 (亚线性时间找出素数个数)
「Meissel-Lehmer 算法」是一种能在亚线性时间复杂度内求出 \(1\sim n\) 内质数个数的一种算法. 在看素数相关论文时发现了这个算法,论文链接:Here. 算法的细节来自 OI w ...
- 找出n个数中出现了奇数次的两个数
如果是找只出现了奇数次的一个数, 那么我们从头异或一遍就可以. 那么如何找出现了奇数次的两个数呢? 首先我们还是从头异或一遍, 然后结果肯定不为0, 对于异或出来的结果, 如果这个数的某一位是1, 说 ...
- 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 ...
- 已知一个数出现的次数超过了一半,请用O(n)的复杂度的算法找出这个数
#include<iostream> using namespace std; //#define maxn 2000010 #include<stdio.h> //int a ...
随机推荐
- CentOS7关闭/开启防火墙出现 Unit iptables.service failed to load
在vm中安装好tomcat,而且在liunx中使用nc命令可以返回成功,但是更换到window中访问不到tomcat的情况,是由于linux防火墙的问题造成的,传统的解决方式有2中 第一种解决方案: ...
- CentOS 7.3 minimal 开启网络服务
CentOS7解决不能上网问题 1.先进入控制台 输入ip addr 2.然后su 获取超级管理员权限 3.编辑网络配置文件 vi /etc/sysonfig/network-scripts/ifc ...
- RMI远程服务调用
数据库:info.sql /* Navicat MySQL Data Transfer Source Server : yuanzhen Source Server Version : 50713 S ...
- Jar程序使用MyBatis集成阿里巴巴druid连接池
在写jar程序,而不是web程序的时候,使用mybatis作为持久层,可以集成POOLED连接池,而阿里的druid不能用,确实很郁闷.不过有办法. 首先准备好数据库配置文件 然后对Druid进行一个 ...
- 算法提高 矩阵乘法 区间DP
这是神题,n <= 1000,如果是极限数据普通的n^3区间DP怎么可能过?可偏偏就过了. 刘汝佳大哥的训练指南上面说的存在nlgn的算法解决矩阵链乘问题,可是百度都找不到.... AC代码 # ...
- docker mysql 主从复制
当然首先 docker pull mysql mkdir /usr/local/mysqlData/master/cnf mkdir /usr/local/mysqlData/master/data ...
- 用线性单元(LinearUnit)实现工资预测的Python3代码
功能:通过样本进行训练,让线性单元自己找到(这就是所谓机器学习)工资计算的规律,然后用两组数据进行测试机器是否真的get到了其中的规律. 原文链接在文尾,文章中的代码为了演示起见,仅根据工作年限来预测 ...
- ImportError: No module named 'Box2D' 解决办法
ImportError: No module named 'Box2D'这个问题是由于缺少gym Openai gym是一个用于开发和比较RL算法的工具包,与其他的数值计算库兼容,如tensorflo ...
- 顺便说说webservice
webservice这玩意框架也挺多的.就这玩意我知道cxf,axis2,jersey.通过jdk也能产生webservie.感觉这东西太多,有时间知道点就写点吧.先挖坑在此
- java特征
java的核心是面向对象,与之相对的是面向过程的编程,在对整个java编程没有足够的理解和运用的情况下恐怕没办法很好的理解这两个概念. 在我的初步理解中,写一个程序就例如做一件事情,面向过程的思想或许 ...