2018年苏州大学复试机试
要求
- 要求用C/C++编程;
- 对程序中必要的地方进行注释。
- 上机规则
- 请在电脑桌面上新建一个文件夹文件夹名为考试姓名(中文);
- 考试完毕后,将所编写的文件放在上述文件中。
第一题(20分)
题目
按下式要求计算e的近似值,n=5由键盘输入。
e = 1+1/1! + 1/2! + 1/3! +…+1/n!
代码
#include <iostream>
using namespace std; double Factorial(int n); int main() { int n = 1; double Sum = 0; cout << "请输入n=" << endl; cin >> n; //计算和 for (int i = 0; i < n; ++i) { Sum += Factorial(i + 1); } cout << "e的计算值为:" << Sum << endl; return 0;
} //计算单个1/n!,返回数值
double Factorial(int n){ double Ele = 1; double Sum_Eone = 0; for (int i = 0; i < n; ++i) { Ele *= i + 1; } Sum_Eone = 1.0 / Ele; return Sum_Eone;
}
结果
第二题(30分)
题目
以下三个问题,要求用函数编写:
有一个班的学生,本学期学了四门课,从键盘输入该班各同学各门课的成绩,假设学生数为3。
- 求出每门课程的平均成绩
- 求出全班的平均成绩
- 求出课程最低分的学生和成绩。
代码
#include <iostream>
#include <iomanip> //用来精确浮点数位数,fixed << setprecision(2),限定小数点后两位 using namespace std; const int NUM_STUDENTS = 3;
const int NUM_COURSES = 4; // 函数声明
void inputGrades(double grades[NUM_STUDENTS][NUM_COURSES]);
void displayCourseAverages(double grades[NUM_STUDENTS][NUM_COURSES]);
void displayClassAverage(double grades[NUM_STUDENTS][NUM_COURSES]);
void findMinScoreStudent(double grades[NUM_STUDENTS][NUM_COURSES], int& minStudent);
double calculateClassTotalScore(double grades[NUM_STUDENTS][NUM_COURSES], int studentIndex); int main() { double grades[NUM_STUDENTS][NUM_COURSES]; // 输入成绩 inputGrades(grades); // 显示每门课程的平均成绩 displayCourseAverages(grades); // 显示全班平均成绩 displayClassAverage(grades); // 找到课程最低分的学生 int minStudent; findMinScoreStudent(grades, minStudent); // 显示结果 cout << "课程最低分的学生是学生 " << minStudent + 1 << ",成绩为:" << endl; for (int j = 0; j < NUM_COURSES; ++j) { cout << "课程 " << j + 1 << ": " << fixed << setprecision(2) << grades[minStudent][j] << endl; } cout << "总成绩: " << fixed << setprecision(2) << calculateClassTotalScore(grades, minStudent) << endl; return 0;
} // 输入成绩
void inputGrades(double grades[NUM_STUDENTS][NUM_COURSES]) { cout << "请输入每位学生的四门课程成绩:" << endl; for (int i = 0; i < NUM_STUDENTS; ++i) { for (int j = 0; j < NUM_COURSES; ++j) { cout << "学生 " << i + 1 << " 课程 " << j + 1 << " 成绩:"; cin >> grades[i][j]; } }
} // 计算每门课程的平均成绩并显示
void displayCourseAverages(double grades[NUM_STUDENTS][NUM_COURSES]) { cout << "每门课程的平均成绩:" << endl; for (int i = 0; i < NUM_COURSES; ++i) { double courseAverage = 0; for (int j = 0; j < NUM_STUDENTS; ++j) { courseAverage += grades[j][i]; } courseAverage /= NUM_STUDENTS; cout << "课程 " << i + 1 << ": " << fixed << setprecision(2) << courseAverage << endl; }
} // 计算全班平均成绩并显示
void displayClassAverage(double grades[NUM_STUDENTS][NUM_COURSES]) { double classAverage = 0; for (int i = 0; i < NUM_STUDENTS; ++i) { for (int j = 0; j < NUM_COURSES; ++j) { classAverage += grades[i][j]; } } classAverage /= (NUM_STUDENTS * NUM_COURSES); cout << "全班平均成绩: " << fixed << setprecision(2) << classAverage << endl;
} // 计算每个学生的总分
double calculateClassTotalScore(double grades[NUM_STUDENTS][NUM_COURSES], int studentIndex) { double totalScore = 0; for (int j = 0; j < NUM_COURSES; ++j) { totalScore += grades[studentIndex][j]; } return totalScore;
} // 找到总分最低的学生
void findMinScoreStudent(double grades[NUM_STUDENTS][NUM_COURSES], int& minStudent) { minStudent = 0; // 初始化为第一个学生 for (int i = 1; i < NUM_STUDENTS; ++i) { double totalScoreCurrent = calculateClassTotalScore(grades, i); double totalScoreMin = calculateClassTotalScore(grades, minStudent); if (totalScoreCurrent < totalScoreMin) { minStudent = i; } }
}
结果
最后
此代码为个人编写,题目参考互联网资源,使用平台为Clion,C++17标准。
由于博主才疏学浅,如有错误请多多指正,如有更好解法请多多交流!