- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
给定一个数组 nums = { 1,2,5,3,6,-1,-2,10,11,12},使用最大元素数(比如 maxNums=3)找到总和(比如总和 =10)= K 的元素
所以如果要使用的 maxNums = 3 求和 = 10答案是
{1 3 6}
{1 -1 10}
{1 -2 11}
{2 5 3}
{2 -2 10}
{5 6 -1}
{-1 11}
{-2 12}
{10}
我写了一个递归函数来完成这项工作。 不使用递归我该怎么做?和/或内存更少?
class Program
{
static Int32[] nums = { 1,2,5,3,6,-1,-2,10,11,12};
static Int32 sum = 10;
static Int32 maxNums = 3;
static void Main(string[] args)
{
Int32[] arr = new Int32[nums.Length];
CurrentSum(0, 0, 0, arr);
Console.ReadLine();
}
public static void Print(Int32[] arr)
{
for (Int32 i = 0; i < arr.Length; i++)
{
if (arr[i] != 0)
Console.Write(" " +arr[i]);
}
Console.WriteLine();
}
public static void CurrentSum(Int32 sumSoFar, Int32 numsUsed, Int32 startIndex, Int32[] selectedNums)
{
if ( startIndex >= nums.Length || numsUsed > maxNums)
{
if (sumSoFar == sum && numsUsed <= maxNums)
{
Print(selectedNums);
}
return;
}
**//Include the next number and check the sum**
selectedNums[startIndex] = nums[startIndex];
CurrentSum(sumSoFar + nums[startIndex], numsUsed+1, startIndex+1, selectedNums);
**//Dont include the next number**
selectedNums[startIndex] = 0;
CurrentSum(sumSoFar , numsUsed , startIndex + 1, selectedNums);
}
}
最佳答案
你的功能看起来不错,但可能有点优化:
class Program
{
static Int32[] nums = { 1, 2, 5, 3, 6, -1, -2, 10, 11, 12 };
static Int32 sum = 10;
static Int32 maxNums = 3;
static Int32[] selectedNums = new Int32[maxNums];
static void Main(string[] args)
{
CurrentSum(0, 0, 0);
Console.ReadLine();
}
public static void Print(int count)
{
for (Int32 i = 0; i < count; i++)
{
Console.Write(" " + selectedNums[i]);
}
Console.WriteLine();
}
public static void CurrentSum(Int32 sumSoFar, Int32 numsUsed, Int32 startIndex)
{
if (sumSoFar == sum && numsUsed <= maxNums)
{
Print(numsUsed);
}
if (numsUsed >= maxNums || startIndex >= nums.Length)
return;
for (int i = startIndex; i < nums.Length; i++)
{
// Include i'th number
selectedNums[numsUsed] = nums[i];
CurrentSum(sumSoFar + nums[i], numsUsed + 1, i + 1);
}
}
}
我还修复了您函数中的一个错误。它在以下测试用例上失败:
{10, 2, -2}
Sum = 10
K = 3
您的函数仅返回 {10}
而不是 {10} 和 {10, 2, -2}
关于c# - 数组中等于 N 的 K 个元素之和,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14575931/
这个问题可能类似于In Angular2 *ngFor iteration, how do I output only unique values from the array?但我的问题是还有更多功
我编写了一个算法来获取 float 的总和,该算法对于整数来说非常有效,但是当我应用于 float 时,我得到的总和是负数。但是我的 float 数组只有正 float 。在这里我发布我的代码,感谢您
我想将这个简单的 for 循环转换为并行循环。它遍历字符串数组(从文本文件读取的 float )并计算总和。 for (int i = 0; i { float tmp; if (f
我正在尝试总结日期差异,一切都很好,除了如果有相同日期我想添加 1,例如,如果起始日期是:01/01/2003到目前为止是 01/01/2003 那么我想添加 1 天,但它没有添加 1 天,而是仅在
这个问题已经有答案了: 已关闭10 年前。 Possible Duplicate: Is JavaScript’s Floating-Point Math Broken? 这将是一个非常基本的计算机科
我刚接触sql,卡住了。我正在尝试计算每个用户走过的(每年)距离总和。我有一个具有以下结构的表(我们称之为 dist_table): rowid user_name date
我刚接触sql,卡住了。我正在尝试计算每个用户走过的(每年)距离总和。我有一个具有以下结构的表(我们称之为 dist_table): rowid user_name date
给定一个正数数组。我想将数组拆分为 2 个不同的子集,以使它们的 gcd(最大公约数)之和最大。 示例数组:{6,7,6,7}。 答案:需要的两个子集是:{6,6}和{7,7};它们各自的 gcd(s
我想在我的数组中求和:
我想将下面的字符串拆分为字母和数字,然后我需要计算数字的总和。我的示例问题是 a[20]={"abcd123dc2"}; 预期输出: abcddc 8 我的代码: int main() { c
为什么 sizeof 运算符返回的结构大小大于该结构成员的总大小? 最佳答案 这是因为添加了填充以满足对齐约束。 Data structure alignment影响程序的性能和正确性: 未对齐的访问
为什么 sizeof 运算符返回的结构大小大于该结构成员的总大小? 最佳答案 这是因为添加了填充以满足对齐约束。 Data structure alignment影响程序的性能和正确性: 未对齐的访问
为什么 sizeof 运算符返回的结构大小大于该结构成员的总大小? 最佳答案 这是因为添加了填充以满足对齐约束。 Data structure alignment影响程序的性能和正确性: 未对齐的访问
为什么 sizeof 运算符返回的结构大小大于该结构成员的总大小? 最佳答案 这是因为添加了填充以满足对齐约束。 Data structure alignment影响程序的性能和正确性: 未对齐的访问
为什么 sizeof 运算符返回的结构大小大于该结构成员的总大小? 最佳答案 这是因为添加了填充以满足对齐约束。 Data structure alignment影响程序的性能和正确性: 未对齐的访问
为什么 sizeof 运算符返回的结构大小大于该结构成员的总大小? 最佳答案 这是因为添加了填充以满足对齐约束。 Data structure alignment影响程序的性能和正确性: 未对齐的访问
为什么 sizeof 运算符返回的结构大小大于该结构成员的总大小? 最佳答案 这是因为添加了填充以满足对齐约束。 Data structure alignment影响程序的性能和正确性: 未对齐的访问
为什么 sizeof 运算符返回的结构大小大于该结构成员的总大小? 最佳答案 这是因为添加了填充以满足对齐约束。 Data structure alignment影响程序的性能和正确性: 未对齐的访问
为什么 sizeof 运算符返回的结构大小大于该结构成员的总大小? 最佳答案 这是因为添加了填充以满足对齐约束。 Data structure alignment影响程序的性能和正确性: 未对齐的访问
为什么 sizeof 运算符返回的结构大小大于该结构成员的总大小? 最佳答案 这是因为添加了填充以满足对齐约束。 Data structure alignment影响程序的性能和正确性: 未对齐的访问
我是一名优秀的程序员,十分优秀!