2023年C语言上机考试选题
1、 编写一个函数来计算组合数$ C_n^m $,即n个元素中选取m个元素的组合数,然后再用阶乘计算出结果。
c
#include <stdio.h>
// 计算阶乘的函数
unsigned long long factorial(int n) {
unsigned long long result = 1;
for (int i = 1; i <= n; ++i) {
result *= i;
}
return result;
}
// 计算组合数的函数
unsigned long long combination(int n, int m) {
return factorial(n) / (factorial(m) * factorial(n - m));
}
int main() {
int n, m;
printf("请输入n和m的值(以空格分隔):");
scanf("%d %d", &n, &m);
if (n < m) {
printf("错误:n必须大于等于m。\n");
return 1;
}
unsigned long long result = combination(n, m);
printf("组合数C(%d, %d)为:%llu\n", n, m, result);
return 0;
}2、利用$ e =1+1/2!+1/3!+..+1/n! $,编程计算e的近似值,直到最后一项的绝对值小于10-5时为止,输出e的值并统计累加的项数。
c
#include <stdio.h>
// 计算阶乘的函数
double factorial(int n) {
double result = 1.0;
for (int i = 2; i <= n; ++i) {
result *= i;
}
return result;
}
int main() {
double e = 1.0;
int n = 1;
double term = 1.0 / factorial(n);
int count = 1;
while (term >= 1e-5) {
e += term;
n++;
term = 1.0 / factorial(n);
count++;
}
printf("e的近似值为:%lf\n", e);
printf("累加的项数为:%d\n", count);
return 0;
}3、 编写程序完成学生成绩管理功能,10个学生三门课程,每个学生三门课程成绩从键盘输入。子函数功能分别实现统计每门课最高分、最低分和平均分;将学生总成绩按照从高到低排序。
c
#include <stdio.h>
#define NUM_STUDENTS 10
#define NUM_COURSES 3
// 计算每门课程的最高分
void highest_score(float scores[NUM_STUDENTS][NUM_COURSES]) {
float max_score[NUM_COURSES] = {0};
for (int j = 0; j < NUM_COURSES; ++j) {
for (int i = 0; i < NUM_STUDENTS; ++i) {
if (scores[i][j] > max_score[j]) {
max_score[j] = scores[i][j];
}
}
printf("课程%d的最高分为:%.2f\n", j + 1, max_score[j]);
}
}
// 计算每门课程的最低分
void lowest_score(float scores[NUM_STUDENTS][NUM_COURSES]) {
float min_score[NUM_COURSES] = {100};
for (int j = 0; j < NUM_COURSES; ++j) {
for (int i = 0; i < NUM_STUDENTS; ++i) {
if (scores[i][j] < min_score[j]) {
min_score[j] = scores[i][j];
}
}
printf("课程%d的最低分为:%.2f\n", j + 1, min_score[j]);
}
}
// 计算每门课程的平均分
void average_score(float scores[NUM_STUDENTS][NUM_COURSES]) {
float sum[NUM_COURSES] = {0};
for (int j = 0; j < NUM_COURSES; ++j) {
for (int i = 0; i < NUM_STUDENTS; ++i) {
sum[j] += scores[i][j];
}
printf("课程%d的平均分为:%.2f\n", j + 1, sum[j] / NUM_STUDENTS);
}
}
// 将学生总成绩按照从高到低排序(冒泡排序)
void sort_total_scores(float scores[NUM_STUDENTS][NUM_COURSES]) {
float total_scores[NUM_STUDENTS];
float temp;
// 计算学生总成绩并存储在数组中
for (int i = 0; i < NUM_STUDENTS; ++i) {
total_scores[i] = 0;
for (int j = 0; j < NUM_COURSES; ++j) {
total_scores[i] += scores[i][j];
}
}
// 冒泡排序
for (int i = 0; i < NUM_STUDENTS - 1; ++i) {
for (int j = 0; j < NUM_STUDENTS - i - 1; ++j) {
if (total_scores[j] < total_scores[j + 1]) {
// 交换学生总成绩
temp = total_scores[j];
total_scores[j] = total_scores[j + 1];
total_scores[j + 1] = temp;
// 交换学生成绩矩阵中对应的行
for (int k = 0; k < NUM_COURSES; ++k) {
temp = scores[j][k];
scores[j][k] = scores[j + 1][k];
scores[j + 1][k] = temp;
}
}
}
}
// 输出排序后的结果
printf("学生总成绩按照从高到低排序如下:\n");
for (int i = 0; i < NUM_STUDENTS; ++i) {
printf("学生%d 总成绩:%.2f\n", i + 1, total_scores[i]);
}
}
int main() {
float scores[NUM_STUDENTS][NUM_COURSES];
// 从键盘输入学生成绩
printf("请输入学生成绩(每行依次为一个学生的三门课程成绩):\n");
for (int i = 0; i < NUM_STUDENTS; ++i) {
printf("学生%d:", i + 1);
for (int j = 0; j < NUM_COURSES; ++j) {
scanf("%f", &scores[i][j]);
}
}
// 调用子函数统计每门课程的最高分、最低分和平均分
highest_score(scores);
lowest_score(scores);
average_score(scores);
// 调用子函数将学生总成绩按照从高到低排序
sort_total_scores(scores);
return 0;
}4、 输入一个整数,分别输出它的每一位数字。
c
#include <stdio.h>
int main() {
int number, digit;
// 从键盘输入一个整数
printf("请输入一个整数:");
scanf("%d", &number);
printf("输入的整数的每一位数字为:\n");
// 从个位开始逐位取出数字并输出
while (number != 0) {
digit = number % 10; // 获取最后一位数字
printf("%d ", digit);
number /= 10; // 去掉最后一位数字
}
printf("\n");
return 0;
}5、用递归的方法把一个数组的元素逆置。
c
#include <stdio.h>
// 交换数组中两个元素的值
void swap(int *a, int *b) {
int temp = *a;
*a = *b;
*b = temp;
}
// 递归函数,用于逆置数组元素
void reverse_array(int arr[], int start, int end) {
// 当起始位置大于等于结束位置时,说明逆置完成
if (start >= end)
return;
// 交换起始位置和结束位置的元素
swap(&arr[start], &arr[end]);
// 递归处理剩余的子数组
reverse_array(arr, start + 1, end - 1);
}
int main() {
int arr[] = {1, 2, 3, 4, 5};
int n = sizeof(arr) / sizeof(arr[0]);
printf("原数组:");
for (int i = 0; i < n; ++i) {
printf("%d ", arr[i]);
}
printf("\n");
// 调用递归函数逆置数组元素
reverse_array(arr, 0, n - 1);
printf("逆置后的数组:");
for (int i = 0; i < n; ++i) {
printf("%d ", arr[i]);
}
printf("\n");
return 0;
}