- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
给定连续奇数组成的三 Angular 形:
1
3 5
7 9 11
13 15 17 19
21 23 25 27 29
//根据行索引(从索引 1 开始)计算该三 Angular 形的行和,例如:
rowSumOddNumbers(1); // 1
rowSumOddNumbers(2); // 3 + 5 = 8
我尝试使用 for 循环来解决这个问题:
function rowSumOddNumbers(n){
let result = [];
// generate the arrays of odd numbers
for(let i = 0; i < 30; i++){
// generate sub arrays by using another for loop
// and only pushing if the length is equal to current j
let sub = [];
for(let j = 1; j <= n; j++){
// if length === j (from 1 - n) keep pushing
if(sub[j - 1].length <= j){
// and if i is odd
if(i % 2 !== 0){
// push the i to sub (per length)
sub.push(i);
}
}
}
// push everything to the main array
result.push(sub);
}
// return sum of n
return result[n + 1].reduce(function(total, item){
return total += item;
});
}
我上面的代码不起作用。基本上,我计划首先生成一个小于 30 的奇数数组。接下来,我需要根据迭代 (j) 的长度(从 1 - n(已通过))创建一个子数组。然后最后push到主数组中。然后使用reduce获得该索引中所有值的总和+1(因为索引从1开始)。
知道我缺少什么以及如何实现这项工作吗?
最佳答案
大多数代码问题首先需要进行一些分析,以便发现模式,然后将其转换为代码。查看三 Angular 形,您会发现每行的总和遵循某种模式:
1: 1 === 1 ^ 3
2: 3 + 5 = 8 === 2 ^ 3
3: 7 + 9 + 11 = 27 === 3 ^ 3
... etc
因此,从上面的分析中,您可以看到您的代码可能会稍微简化 - 我不会发布答案,但请考虑使用 Math.pow
。
关于javascript - 奇数数组之和 - JS,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47607866/
这个问题可能类似于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影响程序的性能和正确性: 未对齐的访问
我是一名优秀的程序员,十分优秀!