C_TA_Website/source/_posts/answer8.md

187 lines
3.3 KiB
Markdown
Raw Permalink Normal View History

2020-12-08 18:20:18 +08:00
---
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");
}
}
}
```