第六次作业和答案
This commit is contained in:
parent
9b8a1318db
commit
f9992eecc9
3 changed files with 341 additions and 19 deletions
267
source/_posts/answer6.md
Normal file
267
source/_posts/answer6.md
Normal file
|
@ -0,0 +1,267 @@
|
|||
---
|
||||
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];
|
||||
|
||||
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;
|
||||
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;
|
||||
for (int i = 0; i < 3; ++i) {
|
||||
for (int j = 0; j < 5; ++j) {
|
||||
sum_xi2 += score[i][j];
|
||||
sum_xi2 += score[i][j] * score[i][j];
|
||||
}
|
||||
}
|
||||
double sigma = sum_xi2 / 15 - sum_xi * sum_xi / 225;
|
||||
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;
|
||||
}
|
||||
```
|
|
@ -6,39 +6,45 @@ date: 2020-10-30 09:39:53
|
|||
tags:
|
||||
---
|
||||
|
||||
# 第六次作业
|
||||
|
||||
- [题目](/~zhaozuohong/homework6/)
|
||||
- [答案](/~zhaozuohong/answer6/)
|
||||
- [讲解](/~zhaozuohong/resources/Experiment6.pdf)
|
||||
|
||||
# 第一次大作业
|
||||
|
||||
* [题目](/~zhaozuohong/big-homework1/)
|
||||
* [答案](/~zhaozuohong/big-answer1/)
|
||||
* [实验报告模板( $\rm \LaTeX$ )](/~zhaozuohong/template1/)
|
||||
- [题目](/~zhaozuohong/big-homework1/)
|
||||
- [答案](/~zhaozuohong/big-answer1/)
|
||||
- [实验报告模板( $\rm \LaTeX$ )](/~zhaozuohong/template1/)
|
||||
|
||||
# 第五次作业
|
||||
|
||||
* [题目](/~zhaozuohong/homework5/)
|
||||
* [答案](/~zhaozuohong/answer5/)
|
||||
* [讲解](/~zhaozuohong/resources/Experiment5.pdf)
|
||||
- [题目](/~zhaozuohong/homework5/)
|
||||
- [答案](/~zhaozuohong/answer5/)
|
||||
- [讲解](/~zhaozuohong/resources/Experiment5.pdf)
|
||||
|
||||
# 第四次作业
|
||||
|
||||
* [题目](/~zhaozuohong/homework4/)
|
||||
* [答案](/~zhaozuohong/answer4/)
|
||||
* [讲解](/~zhaozuohong/resources/Experiment4.pdf)
|
||||
- [题目](/~zhaozuohong/homework4/)
|
||||
- [答案](/~zhaozuohong/answer4/)
|
||||
- [讲解](/~zhaozuohong/resources/Experiment4.pdf)
|
||||
|
||||
# 第三次作业
|
||||
|
||||
* [题目](/~zhaozuohong/homework3/)
|
||||
* [答案](/~zhaozuohong/answer3)
|
||||
* [测试数据](/~zhaozuohong/test3)
|
||||
* [讲解](/~zhaozuohong/resources/Experiment3.pdf)
|
||||
- [题目](/~zhaozuohong/homework3/)
|
||||
- [答案](/~zhaozuohong/answer3)
|
||||
- [测试数据](/~zhaozuohong/test3)
|
||||
- [讲解](/~zhaozuohong/resources/Experiment3.pdf)
|
||||
|
||||
# 第二次作业
|
||||
|
||||
* [题目](/~zhaozuohong/homework2/)
|
||||
* [答案](/~zhaozuohong/answer2)
|
||||
* [讲解](/~zhaozuohong/resources/Experiment2.pptx)
|
||||
- [题目](/~zhaozuohong/homework2/)
|
||||
- [答案](/~zhaozuohong/answer2)
|
||||
- [讲解](/~zhaozuohong/resources/Experiment2.pptx)
|
||||
|
||||
# 第一次作业
|
||||
|
||||
* [题目](/~zhaozuohong/homework1/)
|
||||
* [答案](/~zhaozuohong/answer1)
|
||||
* [讲解](/~zhaozuohong/resources/Experiment1.pptx)
|
||||
- [题目](/~zhaozuohong/homework1/)
|
||||
- [答案](/~zhaozuohong/answer1)
|
||||
- [讲解](/~zhaozuohong/resources/Experiment1.pptx)
|
||||
|
|
49
source/_posts/homework6.md
Normal file
49
source/_posts/homework6.md
Normal file
|
@ -0,0 +1,49 @@
|
|||
---
|
||||
title: 第六次作业(题目)
|
||||
date: 2020-11-20 12:26:17
|
||||
tags:
|
||||
mathjax: true
|
||||
---
|
||||
|
||||
# 冒泡排序
|
||||
|
||||
对 10 个伪随机数冒泡排序,伪随机数产生方法可参考上面习题指导第 127 页,注意冒泡排序排好了要能提前退出。
|
||||
|
||||
# 矩阵乘法
|
||||
|
||||
实现两个矩阵相乘。
|
||||
|
||||
# P166 T13
|
||||
|
||||
编一程序,将两个字符串连接起来,不要用`strcat`函数。
|
||||
|
||||
# P216 T13
|
||||
|
||||
用递归方法求 $n$ 阶勒让德多项式的值,递归公式为:
|
||||
|
||||
$$
|
||||
\begin{equation*}
|
||||
P_{n}(x) = \left \{
|
||||
\begin{array}{ll}
|
||||
1 & (n = 0) \\
|
||||
x & (n = 1) \\
|
||||
((2n-1) \times x P_{n-1}(x) - (n-1) \times P_{n-2}(x) ) / n & (n \geq 1)
|
||||
\end{array}
|
||||
\right .
|
||||
\end{equation*}
|
||||
$$
|
||||
|
||||
# P216 T14
|
||||
|
||||
输入 5 个学生 3 门课的成绩,分别用函数实现下列功能:
|
||||
|
||||
1. 计算每个学生的平均分;
|
||||
2. 每门课的平均分;
|
||||
3. 找出所有 15 个中最高的所对应的学生和课程;
|
||||
4. 计算平均分方差。
|
||||
|
||||
$$
|
||||
\begin{equation*}
|
||||
\sigma = \frac{1}{n} \sum x_i^2 - \left ( \frac{\sum x_i}{n} \right ) ^2
|
||||
\end{equation*}
|
||||
$$
|
Loading…
Reference in a new issue