Lesson 9。

This commit is contained in:
unlockable
2022-11-11 23:49:31 +08:00
parent 777e33d692
commit b4c35a325c
5 changed files with 235 additions and 0 deletions

40
09/Exercise01.c Normal file
View File

@@ -0,0 +1,40 @@
#include <stdio.h>
#include <stdlib.h>
int findMaxFirst(int sheep[]) {
int max = 0;
int i = 0;
max = sheep[0];
for (i = 1; i < 100; i++) {
if (sheep[i] > max) {
max = sheep[i];
}
}
return max;
}
int findMaxRecursive(int sheep[], int start, int prevMax) {
if (start == 100) {
return prevMax;
}
else {
return findMaxRecursive(sheep, start+1, sheep[start] > prevMax? sheep[start]:prevMax);
}
}
int main() {
int sheep[100] = {0};
int i = 0;
int max1 = 0, max2 = 0;
for (i = 0; i < 100; i++) {
sheep[i] = rand() % 1000;
}
// for (i = 0; i < 100; i++) {
// printf("%d\n", sheep[i]);
// }
max1 = findMaxFirst(sheep);
max2 = findMaxRecursive(sheep, 0, 0);
printf("By first method, the max is %d\n", max1);
printf("By second method, the max is %d", max2);
return 0;
}

91
09/Exercise02.c Normal file
View File

@@ -0,0 +1,91 @@
#include <stdio.h>
#include <stdbool.h>
double matA[4][4] = {
{1.1161, 0.1254, 0.1397, 0.1490},
{0.1582, 1.1675, 0.1768, 0.1871},
{0.2368, 0.2471, 0.2568, 1.2671},
{0.1968, 0.2071, 1.2168 ,0.2271}
};
double matB[4] = {
1.5471, 1.6471, 1.8471, 1.7571
};
void rowManipulation(int originRow, double coefficent, int rowBeingAdded) {
int col = 0;
for (col = 0; col < 4; col++) {
matA[rowBeingAdded][col] += matA[originRow][col] * coefficent;
}
matB[rowBeingAdded] += matB[originRow] * coefficent;
}
void timesOfRow(int rowCount, double time) {
int col = 0;
for (col = 0; col < 4; col++) {
matA[rowCount][col] *= time;
}
matB[rowCount] *= time;
}
int findNoneZeroItem(int startRowIndex, int columnIndex) {
bool found = false;
int pos = startRowIndex;
for (pos = startRowIndex; pos < 4; pos++) {
if (matA[pos][columnIndex] != 0) {
return pos;
}
}
return -1;
}
void swapRow(int firstRowIndex, int secondRowIndex) {
double temp = 0;
int i = 0;
for (i = 0; i < 4; i++) {
temp = matA[firstRowIndex][i];
matA[firstRowIndex][i] = matA[secondRowIndex][i];
matA[secondRowIndex][i] = temp;
}
temp = matB[firstRowIndex];
matB[firstRowIndex] = matB[secondRowIndex];
matB[secondRowIndex] = temp;
}
void printOut() {
int i = 0, j = 0;
for (i = 0; i < 4; i++) {
for (j = 0; j < 4; j++) {
printf("%lf\t", matA[i][j]);
}
printf("%lf", matB[i]);
printf("\n");
}
}
int main() {
int x[4] = {0};
int column = 0, row = 0;
int tempRow = 0;
int noneZeroRow = 0;
for (column = 0; column < 4; column++) {
noneZeroRow = findNoneZeroItem(row, column);
if (noneZeroRow == -1) {
continue;
}
else {
swapRow(row, noneZeroRow);
timesOfRow(row, 1.0/matA[row][column]);
for (tempRow = 0; tempRow < 4; tempRow++) {
if (tempRow != row)
rowManipulation(row, -matA[tempRow][column], tempRow);
}
row++;
}
}
for (row = 0; row < 4; row++) {
printf("x(%d) = %lf\n", row+1, matB[row]);
}
return 0;
}

41
09/Optional01.c Normal file
View File

@@ -0,0 +1,41 @@
#include <stdio.h>
#include <stdbool.h>
int history[1000] = {0};
int nextIndex = 0;
int inHistory(int num) {
int i = 0;
for (i = 0; i < nextIndex; i++) {
if (history[i] == num) {
return true;
}
}
return false;
}
int main() {
int input = 0;
int sum = 0;
int singleDigit = 0;
scanf("%d", &input);
do {
sum = 0;
while (input > 0) {
singleDigit = input % 10;
sum += singleDigit * singleDigit;
input /= 10;
}
input = sum;
if (inHistory(sum)) {
history[nextIndex] = 0;
}
else {
history[nextIndex] = sum;
}
nextIndex++;
}
while (history[nextIndex-1] > 1);
printf("%d\n", history[nextIndex-1]);
return 0;
}

33
09/Optional02.c Normal file
View File

@@ -0,0 +1,33 @@
#include <stdio.h>
void fun(int a[], int n, int flag) {
int t, i, j, k;
for (i = 0; i < n - 1; i++) {
k = i;
for (j = i + 1; j < n; j++) {
if (flag ? a[k] > a[j]: a[k] < a[j]) {
k = j;
}
if (k != i) {
t = a[k];
a[k] = a[i];
a[i] = t;
// Swap a[k] and a[i]
}
}
}
}
int main() {
int c[10] = {8, 6, 7, 10, 9, 3, 5, 2, 4, 1}, i;
fun(c, 5, 0);
//fun(c + 5, 5, 1);
for (i = 0; i < 10; i++) {
printf("%d, ", c[i]);
}
//9, 10, 8, 7, 6, 1, 2, 3, 4, 5,
//一个莫名其妙的排序,它不一定能给出正确的顺序。
//老师出这个题的意义可能只是想强调c是一个指针加5就代表着c[5]的地址?
return 0;
}

30
09/Optional03.c Normal file
View File

@@ -0,0 +1,30 @@
#include <stdio.h>
int main() {
char idCardNo[18] = {0};
int i = 0, sum = 0;
int temp = 0;
char checkSum = 0;
int weigh[17] = {7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2};
for (i = 0; i < 18; i++) {
idCardNo[i] = getchar();
}
for (i = 0; i < 17; i++) {
temp = ((idCardNo[i] - '0')*weigh[i]) % 11;
sum += ((idCardNo[i] - '0')*weigh[i]) % 11;
}
checkSum = (12 - (sum % 11)) % 11;
if (checkSum == 10) {
checkSum = 'X';
}
else {
checkSum += '0';
}
if (checkSum == idCardNo[17]) {
printf("合法\n");
}
else {
printf("非法,正确校验位是%c", checkSum);
}
return 0;
}