From a2253647f45a6ecad02112e5d1b834da5fce4d9f Mon Sep 17 00:00:00 2001 From: hairrrrr <781728963@qq.com> Date: Sat, 7 Mar 2020 20:12:01 +0800 Subject: [PATCH] 3-7 --- .../test/01/01 打印杨辉三角/test1.c | 29 +++++++ .../test/01/01 打印杨辉三角/test2.c | 37 ++++++++ .../test/01/01 打印杨辉三角/题目.md | 12 +++ .../advanced C/test/01/02 字符串旋转/test1.c | 63 ++++++++++++++ .../advanced C/test/01/02 字符串旋转/题目.md | 12 +++ .../advanced C/test/01/03 字符串左旋/test1.c | 56 ++++++++++++ .../advanced C/test/01/04 杨氏矩阵/test1.c | 85 +++++++++++++++++++ .../advanced C/test/01/06 猜凶手/test1.c | 37 ++++++++ .../advanced C/test/01/07 猜名次/test1.c | 48 +++++++++++ 9 files changed, 379 insertions(+) create mode 100644 code/answear/advanced C/test/01/01 打印杨辉三角/test1.c create mode 100644 code/answear/advanced C/test/01/01 打印杨辉三角/test2.c create mode 100644 code/answear/advanced C/test/01/01 打印杨辉三角/题目.md create mode 100644 code/answear/advanced C/test/01/02 字符串旋转/test1.c create mode 100644 code/answear/advanced C/test/01/02 字符串旋转/题目.md create mode 100644 code/answear/advanced C/test/01/03 字符串左旋/test1.c create mode 100644 code/answear/advanced C/test/01/04 杨氏矩阵/test1.c create mode 100644 code/answear/advanced C/test/01/06 猜凶手/test1.c create mode 100644 code/answear/advanced C/test/01/07 猜名次/test1.c diff --git a/code/answear/advanced C/test/01/01 打印杨辉三角/test1.c b/code/answear/advanced C/test/01/01 打印杨辉三角/test1.c new file mode 100644 index 0000000..a1c3ab5 --- /dev/null +++ b/code/answear/advanced C/test/01/01 打印杨辉三角/test1.c @@ -0,0 +1,29 @@ +#include + +#define HEIGHT 10 //ǵĸ߶10 + +int main(void) { + + int YHtrangle[HEIGHT][HEIGHT] = { 0 }; + int i, j; + + // + for (i = 0; i < HEIGHT; i++) { + + YHtrangle[i][0] = 1;//ÿеһԪΪ 1 + YHtrangle[i][i] = 1;//ÿһԪΪ 1 + //ҪǴӵпʼi == 2 + for (j = 1; j < i; j++) + YHtrangle[i][j] = YHtrangle[i - 1][j - 1] + YHtrangle[i - 1][j];// е 2 ڶ 1 + 1ĺͣǵĹ + } + + // + for (i = 0; i < HEIGHT; i++) { + for (j = 0; j <= i; j++) { + printf("%d ", YHtrangle[i][j]); + } + printf("\n"); + } + + return 0; +} \ No newline at end of file diff --git a/code/answear/advanced C/test/01/01 打印杨辉三角/test2.c b/code/answear/advanced C/test/01/01 打印杨辉三角/test2.c new file mode 100644 index 0000000..895cb7c --- /dev/null +++ b/code/answear/advanced C/test/01/01 打印杨辉三角/test2.c @@ -0,0 +1,37 @@ +#include + +#define HEIGHT 10 //Ҫ޸ǵĸ߶޸ + +#define ARRLEN HEIGHT + 1 + +void printYH(int arr[][ARRLEN]) { + + int row, col; + + //ȽϷ˼ά⣬вã± 1 ʼ + for (row = 1; row <= HEIGHT; row++) { + + for (col = 1; col <= row; col++) { + + if (col == 1 || col == row) + arr[row][col] = 1; + else + arr[row][col] = arr[row - 1][col - 1] + arr[row - 1][col]; + + //ֱҪһѭ + printf("%d ", arr[row][col]); + } + + printf("\n"); + } + //ȻҲȱ㣬ڳʹ飬Ҫʱ̼ǵ±Ϊ 0 Dzκݵ +} + +int main(void) { + + int YHtrangle[ARRLEN][ARRLEN] = { 0 }; + + printYH(YHtrangle); + + return 0; +} \ No newline at end of file diff --git a/code/answear/advanced C/test/01/01 打印杨辉三角/题目.md b/code/answear/advanced C/test/01/01 打印杨辉三角/题目.md new file mode 100644 index 0000000..d397948 --- /dev/null +++ b/code/answear/advanced C/test/01/01 打印杨辉三角/题目.md @@ -0,0 +1,12 @@ + +#### 1.打印杨辉三角 + +```c +1 +1 1 +1 2 1 +1 3 3 1 +1 4 6 4 1 + ... +``` + diff --git a/code/answear/advanced C/test/01/02 字符串旋转/test1.c b/code/answear/advanced C/test/01/02 字符串旋转/test1.c new file mode 100644 index 0000000..44256c4 --- /dev/null +++ b/code/answear/advanced C/test/01/02 字符串旋转/test1.c @@ -0,0 +1,63 @@ +#include +#include +#include + +int strJudge(const char* str1, const char* str2) { + + char* str2_start = (char*)str2; + + //str1 str2 ǿָ + assert(str1 != NULL && str2 != NULL); + + //ַ 1 жϾû + assert(strlen(str1) != 1); + + //ַȶһ϶ + if (!(strlen(str1) == strlen(str2))) + return 0; + + // str2 Ѱ str1 ĵһԪ + while (*str2) { + if (*str2 == *str1) { + ++str1;// str1 еĵһԪѾҵһѭѰ str1 еһ + break; + } + + ++str2; + } + //ûҵֱӷ 0 + if (*str2 == '\0') + return 0; + + while (*str1) { + + ++str2; + + if (*str2 == '\0') + str2 = str2_start; + + if (!(*str2 == *str1)) + return 0; + + ++str1; + + } + + return 1; +} + +int main(void) { + + char str1[100]; + char str2[100]; + + printf("Enter two strings: "); + scanf("%s %s", str1, str2); + + if (strJudge(str1, str2)) + printf("Yes\n"); + else + printf("No\n"); + + return 0; +} diff --git a/code/answear/advanced C/test/01/02 字符串旋转/题目.md b/code/answear/advanced C/test/01/02 字符串旋转/题目.md new file mode 100644 index 0000000..33494e2 --- /dev/null +++ b/code/answear/advanced C/test/01/02 字符串旋转/题目.md @@ -0,0 +1,12 @@ +#### 2. 字符串旋转 + + + +写一个函数,判断一个字符串是否为另外一个字符串旋转之后的字符串。 + +例如:给定s1 =AABCD和s2 = BCDAA,返回1 +给定s1=abcd和s2=ACBD,返回0. + +AABCD左旋一个字符得到ABCDA +AABCD左旋两个字符得到BCDAA +AABCD右旋一个字符得到DAABC \ No newline at end of file diff --git a/code/answear/advanced C/test/01/03 字符串左旋/test1.c b/code/answear/advanced C/test/01/03 字符串左旋/test1.c new file mode 100644 index 0000000..5bc335e --- /dev/null +++ b/code/answear/advanced C/test/01/03 字符串左旋/test1.c @@ -0,0 +1,56 @@ +//ʵһַеkַ +// +//磺 +//ABCDһַõBCDA +//ABCDַõCDAB + +#include +#include +#include + +char* strRightReverse(char* str, int num) { + + char strAfterReverse[100] = {0}; + char* strReverse = str; + char* strNotReverse = str; + int i; + int length = strlen(str); + + if (num < 0 || num > strlen(str)) { + printf("Illegal input!\n"); + return str; + } + + //ȽŽ strReverse + strNotReverse += num; + + for (i = 0; i < length - num; i++) + strAfterReverse[i] = *strNotReverse++; + + //ٽҪηβ + for (i = length - num; i < length; i++) { + strAfterReverse[i] = *strReverse++; + } + strAfterReverse[length] = '\0'; + + strcpy(str, strAfterReverse); + + return str; +} + +int main(void) { + + char str[100]; + int num = 0; + + printf("Enter a string: "); + scanf("%s", str); + printf("Enter a number: "); + scanf("%d", &num); + + strRightReverse(str, num); + + printf("output: %s\n", str); + + return 0; +} diff --git a/code/answear/advanced C/test/01/04 杨氏矩阵/test1.c b/code/answear/advanced C/test/01/04 杨氏矩阵/test1.c new file mode 100644 index 0000000..d27f031 --- /dev/null +++ b/code/answear/advanced C/test/01/04 杨氏矩阵/test1.c @@ -0,0 +1,85 @@ +//#### 4. Ͼ +// +//һ־󣬾ÿдǵģϵǵģдľвijǷڡ +// +// +// +//Ҫʱ临ӶСO(N); +// +//*ȲȥܸӶ⣬һַ㷨[ο](https://blog.csdn.net/sgbfblog/article/details/7745450?depth_1-utm_source=distribute.pc_relevant_right.none-task&utm_source=distribute.pc_relevant_right.none-task)* + + +#include +#include + +#define ROW 3 +#define COL 3 + +void printArr(int arr[ROW][COL]); +int searchYangMatri(int arr[ROW][COL], int row, int col, int target, int position[2]); + +int main(void) { + + int target; + int position[2] = {-1, -1};//Ŀھеλ + + //ע⣺Ͼ󲢲 + int arr[ROW][COL] = { + {1, 3, 5}, + {2, 4, 6}, + {7, 8, 9}, + }; + + printArr(arr); + + printf("Enter a target: "); + scanf("%d", &target); + + if (searchYangMatrix(arr, ROW, COL, target, position)) + printf("%d is found!The position in the matrix is as follow:(%d, %d)\n", target, position[0], position[1]); + else + printf("Not Found!\n"); + + + return 0; +} + +void printArr(int arr[ROW][COL]) { + + int i, j; + + for (i = 0; i < ROW; i++) { + for (j = 0; j < COL; j++) + printf("%d ", arr[i][j]); + printf("\n"); + } + +} + +int searchYangMatrix(int arr[ROW][COL], int row, int col, int target, int position[2]) { + + assert(arr != NULL); + + //ӾϽǿʼѰ + int i = 0; + int j = col - 1; + + while (i < ROW && j >= 0) { + + //ĿϽǵȥһУΪϽǵһģ + if (target > arr[i][j]) + i++; + //ĿϽǵСȥһУΪϽǵһСģ + else if (target < arr[i][j]) + j--; + else { + position[0] = j;//½ǵԪطڶλϵԭϣ x ǶӦģx = colԪλõ col + position[1] = row - 1 - i;// y еĹϵ y = ROW - 1 - rowԪλõ row + return 1; + } + + } + + return 0; + +} diff --git a/code/answear/advanced C/test/01/06 猜凶手/test1.c b/code/answear/advanced C/test/01/06 猜凶手/test1.c new file mode 100644 index 0000000..c6714a0 --- /dev/null +++ b/code/answear/advanced C/test/01/06 猜凶手/test1.c @@ -0,0 +1,37 @@ +//#### 6. ձijطһıɱͨŲȷɱֱΪ4ɷһ +// +//Ϊ4ɷĹ: +// +// +// +//A˵ҡ +// +//B˵C +// +//C˵D +// +//D˵Cں˵ +// +//֪3˵滰1˵Ǽٻ +// +// +// +//ЩϢдһȷ˭֡ + +#include + +int main(void) { + + int i, murder; + + //͵˼·١п + + for (murder = 'A'; murder <= 'D'; murder++) { + // 3 ˵滰 1 ˵˼ٻ˵ĸ 3 1 Ϊ 3 + if (((murder != 'A') + (murder == 'C') + (murder == 'D') + (murder != 'D')) == 3 ) + + printf("murder is %c\n", murder); + } + + return 0; +} diff --git a/code/answear/advanced C/test/01/07 猜名次/test1.c b/code/answear/advanced C/test/01/07 猜名次/test1.c new file mode 100644 index 0000000..874219c --- /dev/null +++ b/code/answear/advanced C/test/01/07 猜名次/test1.c @@ -0,0 +1,48 @@ +//#### 7. +// +//5λ˶Աμ10̨ˮԤ +// +//Aѡ˵Bڶҵ +// +//Bѡ˵ҵڶEģ +// +//Cѡ˵ҵһDڶ +// +//Dѡ˵Cҵ +// +//Eѡ˵ҵģAһ +// +//ÿλѡֶ˵һ룬ȷΡ + +#include + +int main(void) { + + int a, b, c, d, e; + + for (a = 1; a <= 5; a++) { + for (b = 1; b <= 5; b++) { + for (c = 1; c <= 5; c++) { + for (d = 1; d <= 5; d++) { + for (e = 1; e <= 5; e++) { + // ÿλѡֶ˵һ + if (((b == 2) + (a == 3)) == 1 + && ((b == 2) + (e == 4)) == 1 + && ((c == 1) + (d == 2)) == 1 + && ((c == 5) + (d == 3)) == 1 + && ((e == 4) + (a == 1)) == 1) { + + //βظ + if (a * b * c * d * e == 120) + printf("A Σ%d\nB Σ%d\nC Σ%d\nD Σ%d\nE Σ%d\n", a, b, c, d, e); + } + + } + } + } + } + } + + return 0; +} +