综合练习c++数组
时间: 2025-05-31 12:25:14 浏览: 15
### C++ 数组综合练习题及答案
以下是几个关于C++数组的综合练习题目及其解答:
---
#### **练习题 1**: 删除重复元素
给定一个整数数组 `nums`,原地删除所有重复的元素,使得每个元素只出现一次,并返回修改后的数组长度。注意:空间复杂度应为 O(1),即不使用额外的空间。
##### 解答:
可以利用双指针法解决此问题。通过遍历数组并将唯一的元素移动到前面部分实现去重效果。
```cpp
#include <iostream>
using namespace std;
int removeDuplicates(int nums[], int size) {
if (size == 0) return 0;
int uniqueIndex = 0; // 记录唯一元素的位置
for (int i = 1; i < size; ++i) {
if (nums[i] != nums[uniqueIndex]) {
nums[++uniqueIndex] = nums[i];
}
}
return uniqueIndex + 1; // 返回新数组的有效长度
}
void displayArray(const int* array, int length) {
for (int i = 0; i < length; ++i) {
cout << array[i] << " ";
}
cout << endl;
}
int main() {
int nums[] = {1, 1, 2, 2, 3, 4, 4, 5};
int size = sizeof(nums) / sizeof(nums[0]);
int newSize = removeDuplicates(nums, size);
cout << "去除重复后的数组:" << endl;
displayArray(nums, newSize);
return 0;
}
```
说明:该程序实现了数组去重功能,并保持了原始顺序[^2]。
---
#### **练习题 2**: 找出两数之和等于目标值的索引
给定一个整数数组 `nums` 和一个目标值 `target`,找出数组中两个数相加等于目标值的索引位置。
##### 解答:
可以通过哈希表优化时间复杂度至 O(n) 来解决问题。
```cpp
#include <iostream>
#include <unordered_map>
using namespace std;
pair<int, int> twoSum(const int nums[], int size, int target) {
unordered_map<int, int> numToIndexMap; // 存储数值对应的下标
for (int i = 0; i < size; ++i) {
int complement = target - nums[i];
if (numToIndexMap.find(complement) != numToIndexMap.end()) {
return make_pair(numToIndexMap[complement], i);
}
numToIndexMap[nums[i]] = i;
}
return {-1, -1}; // 如果找不到则返回错误标志
}
int main() {
int nums[] = {2, 7, 11, 15};
int size = sizeof(nums) / sizeof(nums[0]);
int target = 9;
pair<int, int> result = twoSum(nums, size, target);
if (result.first != -1 && result.second != -1) {
cout << "找到两数之和为目标值的索引分别为: ["
<< result.first << ", " << result.second << "]" << endl;
} else {
cout << "未找到符合条件的结果" << endl;
}
return 0;
}
```
说明:这段代码展示了如何高效寻找满足条件的一对数字及其索引[^2]。
---
#### **练习题 3**: 统计学生成绩
已知一组学生的三门课程成绩存储在一个二维数组中,请分别计算每个人的总分以及每门课的平均分。
##### 解答:
这是一个典型的二维数组操作案例,涉及嵌套循环结构的应用。
```cpp
#include <iostream>
using namespace std;
const int STUDENTS = 4;
const int SUBJECTS = 3;
int main() {
// 初始化学生分数矩阵
int scores[STUDENTS][SUBJECTS] = {{85, 100, 93},
{92, 98, 96},
{90, 88, 100},
{95, 93, 98}};
// 输出每位同学的成绩总计
for (int student = 0; student < STUDENTS; ++student) {
int totalScore = 0;
for (int subject = 0; subject < SUBJECTS; ++subject) {
totalScore += scores[student][subject];
}
cout << "第" << student + 1 << "位学生的总分为:" << totalScore << endl;
}
// 输出各科目平均分
for (int subject = 0; subject < SUBJECTS; ++subject) {
double averageScore = 0.0;
for (int student = 0; student < STUDENTS; ++student) {
averageScore += scores[student][subject];
}
averageScore /= STUDENTS;
cout << "第" << subject + 1 << "科目的平均分为:" << fixed << averageScore << endl;
}
return 0;
}
```
说明:本例演示了如何处理二维数据集中的行列汇总运算逻辑[^1]。
---
#### **练习题 4**: 幂次求和
定义类 `SP` 实现方法 `fun()` 功能如下:对于正整数 n 和 k,计算 \( S = \sum_{i=1}^{n}{i^k} \) 的值。
##### 解答:
这里提供了一个基于对象封装的方式完成幂次累加的任务。
```cpp
#include <iostream>
using namespace std;
class SP {
private:
int n, k;
public:
SP(int n1, int k1): n(n1), k(k1){}
int power(int base, int exponent){
int res = 1;
while(exponent--){
res *= base;
}
return res;
}
int fun(){
int sum = 0;
for(int i = 1; i <= this->n; ++i){
sum += power(i, this->k);
}
return sum;
}
void showResult(){
cout << "前" << n << "项自然数的" << k << "次方和为:"<< fun()<<endl;
}
};
int main(){
SP spInstance(8, 5);
spInstance.showResult();
return 0;
}
```
说明:此类设计模式有助于理解面向对象编程思想下的成员函数调用机制[^3]。
---
###
阅读全文
相关推荐

















