JavaScript 计算数组中的正数和负数程序
给定一个数字数组。编写一个JavaScript程序来查找数组中的正数和负数。
例子:
输入: numbers_array1= [10,20, -1,22,99,20, -9]
输出: 正数个数=5,负数个数=2
输入: numbers_array2= [-121, – 78, -13, 54, -23]
输出: 正数个数=1,负数个数=4
例子 1: 这段代码使用JavaScript从给定的数组中计算正数和负数。使用for循环遍历列表中的每个元素,并检查是否符合条件(num >= 0),这是检查负数的条件。如果满足条件,则增加负数计数,否则增加正数计数。
JavaScript
<script>
var numbers=[10,-12,89,56,-83,8,90,-8]
var pos_count=neg_count=0
for(let i=0;i<numbers.length;i++)
{
if (numbers[i]<0)
neg_count++;
else
pos_count++;
}
console.log(`The positive numbers in an array is {pos_count}`)
console.log(`The negative numbers in an array is{neg_count}`)
</script>
输出
The positive numbers in an array is 5
The negative numbers in an array is 3
示例2 : 下面的代码使用JavaScript的while循环。
Javascript
<script>
var numbers=[7,-8,55,-87,28,74,-21,54,4]
var pos_count=neg_count=i=0
while(i<numbers.length)
{
if (numbers[i]<0)
neg_count++;
else
pos_count++;
i++;
}
console.log(`The positive numbers in an array is {pos_count}`)
console.log(`The negative numbers in an array is{neg_count}`)
</script>
输出
The positive numbers in an array is 6
The negative numbers in an array is 3
示例 3 :以下代码使用JavaScript,使用 forEach 循环
Javascript
var numbers=[-8,10,23,44,-80,-15,-13,-1]
var pos_count=neg_count=0
numbers.forEach(element => {
if (element<0)
neg_count++;
else
pos_count++;
});
console.log(`The positive numbers in an array is {pos_count}`)
console.log(`The negative numbers in an array is{neg_count}`)
输出
The positive numbers in an array is 3
The negative numbers in an array is 5
例子4:使用map方法
Javascript
var numbers=[10,-12,89,56,-83,8,90,-8]
var pos_count=neg_count=0
numbers.map(function(element)
{
if (element<0)
neg_count++;
else
pos_count++;
});
console.log(`The positive numbers in an array is {pos_count}`)
console.log(`The negative numbers in an array is{neg_count}`)
输出
The positive numbers in an array is 5
The negative numbers in an array is 3
示例5: 使用递归
JavaScript
function countNumbers(numbers, index, pos_count, neg_count) {
if (index < numbers.length) {
if (numbers[index] < 0) {
neg_count++;
} else {
pos_count++;
}
return countNumbers(numbers, index + 1, pos_count, neg_count);
} else {
return { pos_count, neg_count };
}
}
var numbers=[-8,10,23,44,-80,-15,-1]
var counts = countNumbers(numbers, 0, 0, 0);
console.log(`The positive numbers in an array is {counts.pos_count}`)
console.log(`The negative numbers in an array is{counts.neg_count}`)
输出
The positive numbers in an array is 3
The negative numbers in an array is 4
高效代码:-
这个代码更高效,因为它消除了递归和随之而来的函数调用的需要,而是使用一个简单的for循环来遍历数组。这应该导致更快的执行和更少的内存使用。
JavaScript
function countNumbers(numbers) {
let pos_count = 0;
let neg_count = 0;
for (let i = 0; i < numbers.length; i++) {
if (numbers[i] < 0) {
neg_count++;
} else {
pos_count++;
}
}
return { pos_count, neg_count };
}
var numbers = [-8, 10, 23, 44, -80, -15, -1]
var counts = countNumbers(numbers);
console.log(`The positive numbers in an array is {counts.pos_count}`)
console.log(`The negative numbers in an array is {counts.neg_count}`)
C++
#include <iostream>
using namespace std;
struct Counts {
int pos_count;
int neg_count;
};
Counts countNumbers(int numbers[], int size) {
Counts counts = {0, 0};
for (int i = 0; i < size; i++) {
if (numbers[i] < 0) {
counts.neg_count++;
} else {
counts.pos_count++;
}
}
return counts;
}
int main() {
int numbers[] = {-8, 10, 23, 44, -80, -15, -1};
int size = sizeof(numbers) / sizeof(numbers[0]);
Counts counts = countNumbers(numbers, size);
cout << "The positive numbers in an array is " << counts.pos_count << endl;
cout << "The negative numbers in an array is " << counts.neg_count << endl;
return 0;
}
Java
class Main {
static class Counts {
int pos_count;
int neg_count;
}
static Counts countNumbers(int[] numbers) {
Counts counts = new Counts();
counts.pos_count = 0;
counts.neg_count = 0;
for (int i = 0; i < numbers.length; i++) {
if (numbers[i] < 0) {
counts.neg_count++;
} else {
counts.pos_count++;
}
}
return counts;
}
public static void main(String[] args) {
int[] numbers = {-8, 10, 23, 44, -80, -15, -1};
Counts counts = countNumbers(numbers);
System.out.println("The positive numbers in an array is " + counts.pos_count);
System.out.println("The negative numbers in an array is " + counts.neg_count);
}
}
C
using System;
class MainClass {
struct Counts {
public int pos_count;
public int neg_count;
}
public static Counts countNumbers(int[] numbers) {
Counts counts = new Counts { pos_count = 0, neg_count = 0 };
for (int i = 0; i < numbers.Length; i++) {
if (numbers[i] < 0) {
counts.neg_count++;
} else {
counts.pos_count++;
}
}
return counts;
}
public static void Main(string[] args) {
int[] numbers = new int[] {-8, 10, 23, 44, -80, -15, -1};
Counts counts = countNumbers(numbers);
Console.WriteLine("The positive numbers in an array is " + counts.pos_count);
Console.WriteLine("The negative numbers in an array is " + counts.neg_count);
}
}
输出
The positive numbers in an array is {counts.pos_count}
The negative numbers in an array is {counts.neg_count}
示例 7:使用 Array.prototype.filter() 方法过滤掉正数和负数
Javascript
// defining the array of numbers
var numbers=[-8,10,23,44,-80,-15,-13,-1]
// creating a new array containing only positive numbers using filter method
var positiveNumbers = numbers.filter(function(number) {
// return only numbers greater than or equal to 0
return number >= 0;
});
// creating a new array containing only negative numbers using filter method
var negativeNumbers = numbers.filter(function(number) {
// return only numbers less than 0
return number < 0;
});
// printing the count of positive numbers in the array
console.log(`The positive numbers in an array is {positiveNumbers.length}`)
// printing the count of negative numbers in the array
console.log(`The negative numbers in an array is{negativeNumbers.length}`)
输出
The positive numbers in an array is 3
The negative numbers in an array is 5
时间复杂度: O(n)
辅助空间复杂度: O(n)