C_TA_Website/source/_posts/answer6.md

268 lines
5.1 KiB
Markdown
Raw Permalink Normal View History

2020-11-21 22:37:53 +08:00
---
title: 第六次作业(答案)
date: 2020-11-20 17:40:16
tags:
---
# 冒泡排序
```C
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define LEN 10
void swap(int a[], int i, int j)
{
int tmp = a[i];
a[i] = a[j];
a[j] = tmp;
}
void bubble_sort(int a[], int n)
{
int done = 0;
for (int i = 0; i < n - 1; ++i) {
if (done)
return;
done = 1;
for (int j = 0; j < n - 1 - i; ++j) {
if (a[j] > a[j + 1]) {
swap(a, j, j + 1);
done = 0;
}
}
}
}
void rand_array(int a[], int n)
{
for (int i = 0; i < n; ++i) {
int tmp = rand() % 20 + 1;
a[i] = tmp;
}
}
void print_array(int a[], int n)
{
for (int i = 0; i < n; ++i) {
printf("%d ", a[i]);
}
printf("\n");
}
int main()
{
srand(time(0));
int a[LEN];
printf("使用随机数填充数组:\n");
rand_array(a, LEN);
print_array(a, LEN);
bubble_sort(a, LEN);
printf("排序过后的数组:\n");
print_array(a, LEN);
return 0;
}
```
# 矩阵乘法
```C
#include <stdio.h>
void input_matrix(double mat[][100], int *row, int *column)
{
printf("行:");
scanf("%d", row);
printf("列:");
scanf("%d", column);
printf("数据:\n");
for (int i = 0; i < *row; ++i) {
for (int j = 0; j < *column; ++j) {
scanf("%lf", &mat[i][j]);
}
}
}
void print_matrix(double mat[][100], int row, int column)
{
for (int i = 0; i < row; ++i) {
for (int j = 0; j < column; ++j) {
printf("%f ", mat[i][j]);
}
printf("\n");
}
}
void matrix_mul(double mat_a[][100], double mat_b[][100], double mat_c[][100],
int a_row, int a_column, int b_column)
{
for (int i = 0; i < a_row; ++i) {
for (int j = 0; j < b_column; ++j) {
mat_c[i][j] = 0;
for (int k = 0; k < a_column; ++k) {
mat_c[i][j] += mat_a[i][k] * mat_b[k][j];
}
}
}
}
int main()
{
double mat_a[100][100];
double mat_b[100][100];
double mat_c[100][100];
int a_row;
int a_column;
int b_row;
int b_column;
printf("请输入矩阵A:\n");
input_matrix(mat_a, &a_row, &a_column);
printf("请输入矩阵B:\n");
input_matrix(mat_b, &b_row, &b_column);
if (a_column != b_row) {
printf("A和B无法相乘!\n");
return 0;
}
matrix_mul(mat_a, mat_b, mat_c, a_row, a_column, b_column);
print_matrix(mat_c, a_row, b_column);
return 0;
}
```
# P166 T13
```C
#include <stdio.h>
#include <string.h>
void mystrcat(char a[], char b[])
{
int i = strlen(a) - 1;
for (int j = 0; b[j] != '\n'; ++i, ++j) {
a[i] = b[j];
}
a[i] = '\0';
}
int main()
{
printf("请输入第一个字符串,回车结束:\n");
char a[200];
fgets(a, 200, stdin);
printf("请输入第二个字符串,回车结束:\n");
char b[100];
fgets(b, 100, stdin);
mystrcat(a, b);
printf("连接之后的字符串是\n%s\n", a);
return 0;
}
```
# P216 T13
```C
#include <stdio.h>
double P(int n, double x)
{
if (n == 0) {
return 1;
} else if (n == 1) {
return x;
} else {
return ((2 * n - 1) * x - P(n - 1, x) - (n - 1) * P(n - 2, x)) / n;
}
}
int main()
{
printf("请输入x: ");
double x;
scanf("%lf", &x);
printf("请输入n: ");
int n;
scanf("%d", &n);
printf("P_n(x) = %lf\n", P(n, x));
return 0;
}
```
# P216 T14
```C
#include <stdio.h>
double score[5][3];
2020-11-24 19:51:19 +08:00
double average[5];
2020-11-21 22:37:53 +08:00
void Average_Student(void)
{
for (int i = 0; i < 5; ++i) {
double avg = 0;
for (int j = 0; j < 3; ++j) {
avg += score[i][j];
}
avg /= 3;
2020-11-24 19:51:19 +08:00
average[i] = avg;
2020-11-21 22:37:53 +08:00
printf("第%d个学生的平均成绩是%f\n", i + 1, avg);
}
}
void Average_Lesson(void)
{
for (int i = 0; i < 3; ++i) {
double avg = 0;
for (int j = 0; j < 5; ++j) {
avg += score[j][i];
}
avg /= 5;
printf("第%d门课程的平均成绩是%f\n", i + 1, avg);
}
}
void Find_Max(void)
{
double max = score[0][0];
int max_i = 0, max_j = 0;
for (int i = 0; i < 5; ++i) {
for (int j = 0; j < 3; ++j) {
if (max < score[i][j]) {
max = score[i][j];
max_i = i;
max_j = j;
}
}
}
printf("成绩最高的是第%d个学生的第%d门课程的成绩。\n", max_i + 1, max_j + 1);
}
void Calc_Var(void)
{
double sum_xi = 0;
double sum_xi2 = 0;
2020-11-24 19:51:19 +08:00
for (int i = 0; i < 5; ++i) {
sum_xi += average[i];
sum_xi2 += average[i] * average[i];
2020-11-21 22:37:53 +08:00
}
2020-11-24 19:51:19 +08:00
double sigma = sum_xi2 / 5 - sum_xi * sum_xi / 25;
2020-11-21 22:37:53 +08:00
printf("方差是%f\n", sigma);
}
int main()
{
for (int i = 0; i < 5; ++i) {
for (int j = 0; j < 3; ++j) {
printf("请输入第%d个学生的第%d门成绩:", i + 1, j + 1);
scanf("%lf", &score[i][j]);
}
}
Average_Student();
Average_Lesson();
Find_Max();
Calc_Var();
return 0;
}
```