2013年苏州大学复试机试
第一题
题目
假设有一堆数字(小于100个)需要对其做如下处理:
- 求平均数
- 求标准差
- 求方差
可用函数实现也可以不用
代码
#include <iostream>
#include <sstream> //字符串流
#include <cmath> //数学运算
using namespace std;
void Input_Function();
void Mean_Function();
void Standard_deviation();const int MAX_SIZE = 100;
int Arr[MAX_SIZE];
int Arr_Sum = 0;
double Average = 0;
int Arr_size = 0;int main() {//输入处理Input_Function();//求平均值Mean_Function();//求标准差和方差Standard_deviation();return 0;
}void Input_Function(){int Temp = 0;Arr_size = 0;cout << "请输入数字(以空格分隔):"<< endl;//读取整行输入string input;getline(cin,input);//使用字符串串流解析整行输入stringstream ss(input);//输入数组操作while (ss >> Temp){if (Arr_size < MAX_SIZE){Arr[Arr_size] = Temp;Arr_size++;} else{cout << "数组已满,无法添加更多元素。" << endl;break;}}//打印数组并求和cout << "输入数为:";for (int i = 0; i < Arr_size; ++i) {Arr_Sum += Arr[i];cout << Arr[i] << " ";}cout << endl;}void Mean_Function(){if (Arr_size > 0) {Average = static_cast<double>(Arr_Sum) / Arr_size;cout << "平均值为:" << Average << endl;} else{cout << "数组为空,无法计算平均值。" << endl;}
}void Standard_deviation(){if (Arr_size > 1){double variance = 0;for (int i = 0; i < Arr_size; ++i) {variance += pow(Arr[i] - Average,2);}variance /= (Arr_size - 1);double std_deviation = sqrt(variance);cout << "方差为:" << variance << endl;cout << "标准差为:" << std_deviation << endl;} else{cout << "数组元素不足以计算方差和标准差。" << endl;}
}
结果
第二题
题目
假设有一个4*3的矩阵,元素自定:
- 求各一元各行元素的平均值
- 求该矩阵转置后的矩阵
- 若一个3乘4的矩阵与其相乘求新矩阵
必须用函数实现
代码
#include <iostream>
using namespace std;
void Inpu_Function();
void Tran_Arr();
void New_Arr();
void Average_lacedelmenets();int Arr_A[4][3];
int Tran_Arr_A[3][4];
int Arr_B[3][4];
int Arr_AB[4][4];int main()
{//输入4*3矩阵A的元素Inpu_Function();// 求各一元各行元素的平均值Average_lacedelmenets();//矩阵A的转置Tran_Arr();//一个3乘4的矩阵与其相乘的新矩阵New_Arr();system("pause");return 0;
}void Inpu_Function()
{cout << "请输入4*3矩阵A的元素:" << endl;//输入矩阵元素:for (int i = 0; i < 4; ++i) {for (int j = 0; j < 3; ++j) {cout << "请输入第 " << (i + 1) << " 行,第 " << (j + 1) << " 列的元素:";cin >> Arr_A[i][j];}}//输出矩阵cout << "输入的矩阵为:" << endl;for (int i = 0; i < 4; ++i) {for (int j = 0; j < 3; ++j) {cout << Arr_A[i][j] << " ";}cout << endl;}
}void Average_lacedelmenets() {cout << "各行元素的平均值为:" << endl;for (int i = 0; i < 4; ++i) {int rowSum = 0;for (int j = 0; j < 3; ++j) {rowSum += Arr_A[i][j];}double average = static_cast<double>(rowSum) / 3;cout << "第 " << (i + 1) << " 行元素的平均值为:" << average << endl;}
}void Tran_Arr(){for (int i = 0; i < 4; ++i) {for (int j = 0; j < 3; ++j) {Tran_Arr_A[j][i] = Arr_A[i][j];}}//输出矩阵A的转置cout << "矩阵A的转置为:" << endl;for (int i = 0; i < 3; ++i) {for (int j = 0; j < 4; ++j) {cout << Tran_Arr_A[i][j] << " ";}cout << endl;}
}void New_Arr(){//输入矩阵Bcout << "请输入3*4矩阵B的元素:" << endl;for (int i = 0; i < 3; ++i) {for (int j = 0; j < 4; ++j) {cout << "请输入第 " << (i + 1) << " 行,第 " << (j + 1) << " 列的元素:";cin >> Arr_B[i][j];}}//计算矩阵A和B的乘积for (int i = 0; i < 4; ++i) {for (int j = 0; j < 4; ++j) {Arr_AB[i][j] = 0;for (int k = 0; k < 3; ++k) {Arr_AB[i][j] += Arr_A[i][k] * Arr_B[k][j];}}}// 输出矩阵乘积cout << "矩阵 A 和 B 的乘积为:" << endl;for (int i = 0; i < 4; ++i) {for (int j = 0; j < 4; ++j) {cout << Arr_AB[i][j] << " ";}cout << endl;}}
结果
最后
此代码为个人编写,题目来自互联网,使用平台为Clion,C++17标准。
由于博主才疏学浅,如有错误请多多指正,如有更好解法请多多交流!