第七和第八次

This commit is contained in:
zhbaor 2020-12-08 18:20:18 +08:00
parent 843f1929ab
commit 5e8ae36141
10 changed files with 576 additions and 5 deletions

View file

@ -0,0 +1,7 @@
<html>
<head><title>301 Moved Permanently</title></head>
<body>
<center><h1>301 Moved Permanently</h1></center>
<hr><center>nginx</center>
</body>
</html>

6
package-lock.json generated
View file

@ -677,9 +677,9 @@
}
},
"hexo-theme-next": {
"version": "8.0.1",
"resolved": "https://registry.npm.taobao.org/hexo-theme-next/download/hexo-theme-next-8.0.1.tgz",
"integrity": "sha1-YI4IXk9tjL0PCtrQ1ebnxcz7FXc="
"version": "8.1.0",
"resolved": "https://registry.npm.taobao.org/hexo-theme-next/download/hexo-theme-next-8.1.0.tgz",
"integrity": "sha1-/BLvnm8/4zosO7+XLx5UYEfjTNQ="
},
"hexo-util": {
"version": "2.4.0",

View file

@ -21,6 +21,6 @@
"hexo-renderer-pandoc": "^0.3.0",
"hexo-renderer-stylus": "^2.0.0",
"hexo-server": "^2.0.0",
"hexo-theme-next": "^8.0.1"
"hexo-theme-next": "^8.1.0"
}
}

250
source/_posts/answer7.md Normal file
View file

@ -0,0 +1,250 @@
---
title: 第七次作业(答案)
date: 2020-11-27 10:53:14
tags:
---
# 交换宏
```C
#include <stdio.h>
#define SWAP(a, b) \
{ \
typeof(a) _t = a; \
a = b; \
b = _t; \
}
int main()
{
int a, b;
printf("请输入a和b:\n");
printf("a = ");
scanf("%d", &a);
printf("b = ");
scanf("%d", &b);
SWAP(a, b);
printf("\n交换后的结果:\na = %d\nb = %d\n", a, b);
return 0;
}
```
# 排序与查找
```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");
}
void insert(int *a, int n, int insIndex, insValue)
{
for (int j = n - 1; j >= insIndex; ++j) {
a[j + 1] = a[j];
}
a[insIndex] = insValue;
}
void binary_search(int *a, int n, int toSearch)
{
int found = 0;
int l = 0;
int r = n - 1;
while (l <= r) {
int c = l + (r - l) / 2;
if (toSearch < a[c]) {
r = c - 1;
} else if (toSearch > a[c]) {
l = c + 1;
} else {
found = 1;
}
}
if (!found) {
insert(a, n, l, toSearch);
}
}
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;
}
```
# P219-T4
```C
#include <stdio.h>
void LeftRotate(int* a, int n, int m)
{
int j = 0;
int oldValue = a[0];
int newValue;
for (int i = 0; i < n; ++i) {
int k = (j + m) % n;
newValue = a[k];
a[k] = oldValue;
oldValue = newValue;
j = k;
}
}
int main()
{
int a[100];
int m, n;
printf("请输入n: \nn = ");
scanf("%d", &n);
printf("请输入m: \nm = ");
scanf("%d", &m);
printf("请输入n个数:\n");
for (int i = 0; i < n; ++i) {
scanf("%d", a + i);
}
printf("经过移动后的数组为:\n");
for (int i = 0; i < n; ++i) {
printf("%d ", a[i]);
}
printf("\n");
return 0;
}
```
# P291-T5
```C
#include <stdio.h>
int find_next(int* a, int n, int i)
{
for (int j = i + 1; j < n; ++j) {
if (a[j] == 1) {
return j;
}
}
for (int j = 0; j < i; ++j) {
if (a[j] == 1) {
return j;
}
}
return -1;
}
int Search(int* a, int n)
{
int num = 0;
int count = 0;
int j = 0;
while (count < n - 1) {
if (num == 2) {
a[j] = 0;
num = 0;
count++;
} else {
num++;
}
j = find_next(a, n, j);
}
return j + 1;
}
int main()
{
int a[100];
int n;
printf("请输入n:\nn = ");
scanf("%d", &n);
for (int i = 0; i < n; ++i) {
a[i] = 1;
}
printf("剩下的是第%d个人。\n", Search(a, n));
return 0;
}
```
# P291-T9
```C
#include <stdio.h>
void Trans(int (*a)[3])
{
for (int i = 1; i < 3; ++i) {
for (int j = 0; j < i; ++j) {
int tmp = a[i][j];
a[i][j] = a[j][i];
a[j][i] = tmp;
}
}
}
int main()
{
int a[3][3];
printf("请输入3x3矩阵的9个元素:\n");
for (int i = 0; i < 3; ++i) {
for (int j = 0; j < 3; ++j) {
scanf("%d", &a[i][j]);
}
}
Trans(a);
printf("转置后:\n");
for (int i = 0; i < 3; ++i) {
for (int j = 0; j < 3; ++j) {
printf("%d ", a[i][j]);
}
printf("\n");
}
return 0;
}
```

186
source/_posts/answer8.md Normal file
View file

@ -0,0 +1,186 @@
---
title: 第八次作业(答案)
date: 2020-12-08 16:29:01
tags:
---
# P292-10
```C
#include <stdio.h>
void swap(int array[], int i, int j)
{
int tmp = array[i];
array[i] = array[j];
array[j] = tmp;
}
int main(void)
{
int mat[5][5];
printf("请输入5x5矩阵的25个元素:\n");
for (int i = 0; i < 5; ++i) {
for (int j = 0; j < 5; ++j) {
scanf("%d", &mat[i][j]);
}
}
int* array = *mat;
for (int i = 0; i < 4; ++i) {
int min = array[0];
for (int j = i + 1; j < 25; ++j) {
if (array[j] < array[i]) {
swap(array, i, j);
}
}
}
int max = array[5];
for (int i = 4; i < 25; ++i) {
if (array[i] > max) {
max = array[i];
}
}
printf("%5d %5d\n %5d \n%5d %5d\n", array[0], array[1], max, array[2], array[3]);
return 0;
}
```
# P292-12
```C
#include <stdio.h>
#include <string.h>
void sort_str(char* str[], int n)
{
for (int i = 0; i < n - 1; ++i) {
int done = 1;
for (int j = 0; j < n - i - 1; ++j) {
if (strcmp(str[j], str[j + 1]) > 0) {
char* tmp = str[j + 1];
str[j + 1] = str[j];
str[j] = tmp;
done = 0;
}
}
if (done) {
break;
}
}
}
int main(void)
{
char inputs[5][100];
for (int i = 0; i < 5; ++i) {
fgets(inputs[i], 100, stdin);
}
char* str[5];
for (int i = 0; i < 5; ++i) {
str[i] = inputs[i];
}
sort_str(str, 5);
for (int i = 0; i < 5; ++i) {
printf("%s", str[i]);
}
return 0;
}
```
# P292-14
```C
#include <stdio.h>
void reverse(int n)
{
int m;
scanf("%d", &m);
if (n > 1) {
reverse(n - 1);
}
printf("%d ", m);
}
int main(void)
{
printf("请问几个数?\nn = ");
int n;
scanf("%d", &n);
printf("请依次输入这些数:\n");
reverse(n);
printf("\n");
return 0;
}
```
# P292-17
```C
#include <stdio.h>
int strcmp(char* p1, char* p2)
{
for (int i = 0;; ++i) {
if (p1[i] == '\0') {
if (p2[i] == '\0') {
return 0;
}
return -1;
}
if (p2[i] == '\0') {
return 1;
}
if (p1[i] > p2[i]) {
return 1;
}
if (p1[i] < p2[i]) {
return -1;
}
}
}
int main(void)
{
char a[100], b[100];
fgets(a, 100, stdin);
fgets(b, 100, stdin);
printf("%d\n", strcmp(a, b));
return 0;
}
```
# PPT第80页思考题
```C
#include <stdio.h>
int (*seek(int (*pnt_row)[3]))[3]
{
int i = 0, (*ret_row)[3];
ret_row = pnt_row + 1;
for (; i < 3; i++) {
if (pnt_row[0][i] < 60) {
ret_row = pnt_row;
break;
}
}
return ret_row;
}
int main()
{
int grade[3][3] = { { 55, 65, 75 }, { 65, 75, 85 }, { 75, 80, 90 } };
int i, j, (*pointer)[3];
for (i = 0; i < 3; i++) {
pointer = seek(grade + i);
if (pointer == grade + i) {
printf("No. %d grade list: ", i + 1);
for (j = 0; j < 3; j++) {
printf("%d ", pointer[0][j]);
}
printf("\n");
}
}
}
```

View file

@ -6,6 +6,18 @@ date: 2020-10-30 09:39:53
tags:
---
# 第八次作业
- [题目](/~zhaozuohong/homework8/)
- [答案](/~zhaozuohong/answer8/)
- [讲解](/~zhaozuohong/resources/Experiment8.pdf)
# 第七次作业
- [题目](/~zhaozuohong/homework7/)
- [答案](/~zhaozuohong/answer7/)
- [讲解](/~zhaozuohong/resources/Experiment7.pdf)
# 第六次作业
- [题目](/~zhaozuohong/homework6/)

View file

@ -47,7 +47,7 @@ $$
有4个圆塔,圆心分别为 $(2,2)$ 、 $(-2,2)$ 、 $(-2,-2)$ 、 $(2,-2)$ ,圆半径为 $1$ ,如下图所示。这4个塔的高度为 $10\mathrm{m}$ ,塔以外无建筑物。今输入任一点的坐标,求该点的建筑高度(塔外的高度为 $0$ )。
{% asset_img P108-12.png This is an image %}
{% asset_img P108-12.png %}
# 解一元二次方程

View file

@ -0,0 +1,37 @@
---
title: 第七次作业(题目)
date: 2020-11-27 02:11:11
tags:
mathjax: true
---
# 交换宏
定义一个带参宏,功能是两个参数的值互换。在主程序中输入两个数作为使用宏的实参,输出已交换后的两个值。
说明:规定宏只可以带两个实参,例如`swap(a, b)`,不能用三个实参。
# 排序与查找
将[上次第一题](/~zhaozuohong/homework6/)改为指针作为参数调用函数排序,在排序函数中对主函数中待排数组进行排序,再用指针为参数进行折半查找和插入,可以用以前的程序加以修改。
# 谭浩强
## 要求
* 全部要用函数实现
* 参数用指针
## P291-T4
有 $n$ 个整数,使前面各数顺序向后移 $m$ 个位置,最后 $m$ 个数变成最前面 $m$ 个数,见下图。写一函数实现以上功能,在主函数中输入 $n$ 个整数和输出调整后的 $n$ 个数。
{% asset_img P291-T4.png %}
## P291-T5
有 $n$ 个人围成一圈,顺序排号,从第 $1$ 个人开始报数,(从 $1$ 到 $3$ 报数),凡报到 $3$ 的人退出圈子,问最后留下的是原来第几号的那位。
## P291-T9
写一函数,将一个 $3 \times 3$ 的整型矩阵转置。要求:用二维数组名,矩阵转置就是 $i$ 行 $j$ 列元素变成 $j$ 行 $i$ 列元素,对所有的 $i, j$ 有效。

Binary file not shown.

After

Width:  |  Height:  |  Size: 43 KiB

View file

@ -0,0 +1,79 @@
---
title: 第八次作业(题目)
date: 2020-12-08 16:08:00
tags:
mathjax: true
---
# P292-10
将一个 $5 \times 5$ 的矩阵中最大的元素放在中心, $4$ 个角分别放 $4$ 个最小元素(顺序为从左到右,从上到下依次从小到大存放),写一函数实现之。用 `main` 函数调用。
# P292-12
在主函数中输入 $5$ 个不等长的字符串。用另一函数对它们进行冒泡排序,用指针数组处理。然后主函数输出这 $5$ 个已排好序的字符串。
# P292-14
将 $n$ 个数按输入时顺序的逆序排列,用函数实现。
# P292-17
写一函数,实现两个字符串的比较。即自己写一个 `strcmp` 函数,函数原型为
```C
int strcmp(char *p1, char *p2);
```
# PPT第80页思考题
某数理化三项竞赛训练组有 $3$ 个人,找出其中至少有一项成绩不合格者。要求使用指针函数实现。
```C
int *seek(int (*pnt_row)[3])
{
int i = 0, *pnt_col;
pnt_col = *(pnt_row + 1);
for (; i < 3; i++) {
if (*(*pnt_row + i) < 60) {
pnt_col = * pnt_row;
break;
}
}
return pnt_col;
}
int main()
{
int grade[3][3] = {{55, 65, 75}, {65, 75, 85}, {75, 80, 90}};
int i, j, *pointer;
for (i = 0; i < 3; i++) {
pointer = seek(grade + i);
if (pointer == *(grade + i)) {
printf("No. %d grade list: ", i + 1);
for (j = 0; j < 3; j++) {
printf("%d ", *(pointer + j));
}
printf("\n");
}
}
}
```
思考:如何改为函数返回行指针?
# PPT第95页例题
命令行参数举例——`echo`命令
```C
#include <stdio.h>
int main(int argc, char* argv[])
{
while (--argc > 0) {
printf("%s%c", *++argv, (argc > 1) ? ' ' : '\n');
}
return 0;
}
```