// 作业8:将数组 [2, 0, 6, 1, 77, 0, 52, 0, 25, 7] 中大于等于 10 的元素选出来,放入新数组
时间: 2023-05-31 13:05:41 浏览: 92
let arr = [2, 0, 6, 1, 77, 0, 52, 0, 25, 7];
let newArr = [];
for (let i = 0; i < arr.length; i++) {
if (arr[i] >= 10) {
newArr.push(arr[i]);
}
}
console.log(newArr); // [77, 52, 25]
相关问题
第五章 作业 用红色粗体显示的题目要求写出算法思路和程序,其余题目要求给出算法思路。 (1)括号检验:输入一个代数表达式,表达式只能含有+,-,*,/,(,)以及0到9的非负整数,设表达式除括号匹配外,再无其他错误。编写算法对输入的表达式进行检验,判断括号匹配是否正确。 例如: 正确的: 1+2+4 (1+2)+4 (1+2) 错误的 (1+)2 (1+2(4+3)) (1+2+3*(4+5())) 1+2+3*(4+5)) (2)有分数1/2,1/3,1/4,1/5,1/6,1/8,1/10,1/12,1/15,求将其中若干个分数相加和恰好等于1的组成方案,并输出。 例如1/2+1/3+1/6 = 1 (3)是否存在一个由1~9组成的9位数,每个数字只能出现一次,且这个9位数由高位到低位前i位能被i整除? (7)一个正整数有可能可以被表示为n(n≥2)个连续的正整数之和,如n=15时, 15=1+2+3+4+5 15=4+5+6 15=7+8 请编写算法,根据输入的任何一个正整数,找出符合这种要求的所有连续正整数序列。
# (1) 括号检验
算法思路:
使用栈来实现,遍历表达式,遇到左括号时将其入栈,遇到右括号时将栈顶元素出栈并与当前右括号进行匹配,如果匹配成功则继续遍历,否则表达式括号匹配失败。最后判断栈是否为空,若为空则括号匹配成功,否则匹配失败。
代码实现:
```python
def check_parentheses(expression):
stack = []
for char in expression:
if char == '(':
stack.append(char)
elif char == ')':
if stack and stack[-1] == '(':
stack.pop()
else:
return False
return not stack
```
# (2) 分数相加
算法思路:
将分数数组中的每个元素转换成小数,然后枚举所有可能的相加方式,对每一种相加方式进行判断,如果相加结果等于1,则输出相加方式。
代码实现:
```python
from fractions import Fraction
def sum_fraction():
fractions = [Fraction(1, 2), Fraction(1, 3), Fraction(1, 4), Fraction(1, 5), Fraction(1, 6), Fraction(1, 8), Fraction(1, 10), Fraction(1, 12), Fraction(1, 15)]
decimals = [float(f) for f in fractions]
for i in range(1, len(fractions) + 1):
for indices in combinations(range(len(fractions)), i):
if sum([decimals[j] for j in indices]) == 1:
print('+'.join([str(fractions[j]) for j in indices]), '=', 1)
```
其中, `combinations` 函数是从 `itertools` 库中导入的,用于生成从给定元素中取出指定数量元素的所有可能组合。
# (3) 整除数
算法思路:
使用回溯法遍历所有可能组合,如果某一组合满足题目条件,则输出该组合。
代码实现:
```python
def is_divisible(number):
for i in range(1, len(number) + 1):
if int(number[:i]) % i != 0:
return False
return True
def find_divisible_numbers():
digits = '123456789'
result = []
def backtrack(path):
if len(path) == len(digits):
if is_divisible(path):
result.append(int(path))
else:
for digit in digits:
if digit not in path:
backtrack(path + digit)
backtrack('')
return result
```
# (7) 连续正整数和
算法思路:
使用双指针法,设左指针为 `left`,右指针为 `right`,初始值均为 1。如果从 `left` 到 `right` 的连续正整数之和小于目标值,则将右指针向右移动一位;如果和大于目标值,则将左指针向右移动一位;如果和等于目标值,则记录当前的连续正整数序列,并将左指针向右移动一位。
代码实现:
```python
def find_continuous_sequences(target):
left, right = 1, 1
result = []
while left <= target // 2:
total = sum(range(left, right + 1))
if total == target:
result.append(list(range(left, right + 1)))
left += 1
elif total < target:
right += 1
else:
left += 1
return result
```
定义一个名为PartFilledArrayWMax的类,它是作业7-1中PartFilledArray类的一个派生类。PartFilledArrayWMax新增了一个名为max_value的成员变量,它容纳数组中存储的所有数的最大值。定义一个名为get_max的成员函数,用它返回数组中存储的最大值。重定义成员函数addValue,用于向数组中添加一个值。并定义两个构造函数,其中一个构造函数要获取一个int参数,它表示数组能容纳的最大元素数量;另一个为拷贝构造函数。定义一个析构函数。 在主函数中创建一个PartFilledArrayWMax类的对象a,依次输入若干个数,以0作为输入结束,如果输入数字的个数小于max_number,则将这些数字输出,输出以空格分割,最后一个数字后也有空格。如果输入数字的个数大于max_number,则提示“Adding to a full array.”同时,分别输出这两个数组的最大值。
下面是PartFilledArrayWMax类的实现代码:
```
#include <iostream>
#include <algorithm>
using namespace std;
class PartFilledArray {
protected:
int *contents; // 存储数组的指针
int max_number; // 数组的最大元素数量
int count; // 数组中已经填充的元素数量
public:
PartFilledArray(int max) : max_number(max), count(0) {
contents = new int[max_number];
}
PartFilledArray(const PartFilledArray& arr) {
max_number = arr.max_number;
count = arr.count;
contents = new int[max_number];
for (int i = 0; i < count; i++) {
contents[i] = arr.contents[i];
}
}
virtual ~PartFilledArray() {
delete[] contents;
}
void addValue(int value) {
if (count < max_number) {
contents[count] = value;
count++;
}
}
void display(ostream& out) const {
for (int i = 0; i < count; i++) {
out << contents[i] << " ";
}
out << endl;
}
};
class PartFilledArrayWMax : public PartFilledArray {
private:
int max_value;
public:
PartFilledArrayWMax(int max) : PartFilledArray(max), max_value(0) {}
PartFilledArrayWMax(const PartFilledArrayWMax& arr) : PartFilledArray(arr), max_value(arr.max_value) {}
void addValue(int value) {
PartFilledArray::addValue(value);
max_value = max(max_value, value);
}
int get_max() const {
return max_value;
}
};
int main() {
int max_number;
cout << "Enter the maximum number of elements: ";
cin >> max_number;
PartFilledArrayWMax a(max_number);
int value;
cout << "Enter numbers (0 to end): ";
do {
cin >> value;
if (value != 0) {
a.addValue(value);
}
} while (value != 0);
cout << "The numbers entered are: ";
a.display(cout);
if (a.get_max() == 0) {
cout << "The array is empty." << endl;
} else {
cout << "The maximum value in the array is: " << a.get_max() << endl;
}
return 0;
}
```
在主函数中,首先让用户输入数组的最大元素数量,然后创建一个PartFilledArrayWMax类的对象a。接着让用户输入若干个数,调用addValue函数将这些数添加到数组中。最后调用display函数输出数组中存储的数字,调用get_max函数输出最大值。如果数组为空,则输出"The array is empty."。
相关推荐
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://img-home.csdnimg.cn/images/20210720083646.png)