mirror of
https://github.com/hairrrrr/C-CrashCourse.git
synced 2026-02-11 06:26:59 +08:00
3-7
This commit is contained in:
29
code/answear/advanced C/test/01/01 打印杨辉三角/test1.c
Normal file
29
code/answear/advanced C/test/01/01 打印杨辉三角/test1.c
Normal file
@@ -0,0 +1,29 @@
|
||||
#include<stdio.h>
|
||||
|
||||
#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;
|
||||
}
|
||||
37
code/answear/advanced C/test/01/01 打印杨辉三角/test2.c
Normal file
37
code/answear/advanced C/test/01/01 打印杨辉三角/test2.c
Normal file
@@ -0,0 +1,37 @@
|
||||
#include<stdio.h>
|
||||
|
||||
#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 是不存任何有意义的数据的
|
||||
}
|
||||
|
||||
int main(void) {
|
||||
|
||||
int YHtrangle[ARRLEN][ARRLEN] = { 0 };
|
||||
|
||||
printYH(YHtrangle);
|
||||
|
||||
return 0;
|
||||
}
|
||||
12
code/answear/advanced C/test/01/01 打印杨辉三角/题目.md
Normal file
12
code/answear/advanced C/test/01/01 打印杨辉三角/题目.md
Normal file
@@ -0,0 +1,12 @@
|
||||
|
||||
#### 1.打印杨辉三角
|
||||
|
||||
```c
|
||||
1
|
||||
1 1
|
||||
1 2 1
|
||||
1 3 3 1
|
||||
1 4 6 4 1
|
||||
...
|
||||
```
|
||||
|
||||
63
code/answear/advanced C/test/01/02 字符串旋转/test1.c
Normal file
63
code/answear/advanced C/test/01/02 字符串旋转/test1.c
Normal file
@@ -0,0 +1,63 @@
|
||||
#include<stdio.h>
|
||||
#include<string.h>
|
||||
#include<assert.h>
|
||||
|
||||
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;
|
||||
}
|
||||
12
code/answear/advanced C/test/01/02 字符串旋转/题目.md
Normal file
12
code/answear/advanced C/test/01/02 字符串旋转/题目.md
Normal file
@@ -0,0 +1,12 @@
|
||||
#### 2. 字符串旋转
|
||||
|
||||
|
||||
|
||||
写一个函数,判断一个字符串是否为另外一个字符串旋转之后的字符串。
|
||||
|
||||
例如:给定s1 =AABCD和s2 = BCDAA,返回1
|
||||
给定s1=abcd和s2=ACBD,返回0.
|
||||
|
||||
AABCD左旋一个字符得到ABCDA
|
||||
AABCD左旋两个字符得到BCDAA
|
||||
AABCD右旋一个字符得到DAABC
|
||||
56
code/answear/advanced C/test/01/03 字符串左旋/test1.c
Normal file
56
code/answear/advanced C/test/01/03 字符串左旋/test1.c
Normal file
@@ -0,0 +1,56 @@
|
||||
//实现一个函数,可以左旋字符串中的k个字符。
|
||||
//
|
||||
//例如:
|
||||
//ABCD左旋一个字符得到BCDA
|
||||
//ABCD左旋两个字符得到CDAB
|
||||
|
||||
#include<stdio.h>
|
||||
#include<string.h>
|
||||
#include<assert.h>
|
||||
|
||||
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;
|
||||
}
|
||||
85
code/answear/advanced C/test/01/04 杨氏矩阵/test1.c
Normal file
85
code/answear/advanced C/test/01/04 杨氏矩阵/test1.c
Normal file
@@ -0,0 +1,85 @@
|
||||
//#### 4. 杨氏矩阵
|
||||
//
|
||||
//有一个数字矩阵,矩阵的每行从左到右是递增的,矩阵从上到下是递增的,请编写程序在这样的矩阵中查找某个数字是否存在。
|
||||
//
|
||||
//
|
||||
//
|
||||
//要求:时间复杂度小于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<stdio.h>
|
||||
#include<assert.h>
|
||||
|
||||
#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;
|
||||
|
||||
}
|
||||
37
code/answear/advanced C/test/01/06 猜凶手/test1.c
Normal file
37
code/answear/advanced C/test/01/06 猜凶手/test1.c
Normal file
@@ -0,0 +1,37 @@
|
||||
//#### 6. 猜凶手日本某地发生了一件谋杀案,警察通过排查确定杀人凶手必为4个嫌疑犯的一个。
|
||||
//
|
||||
//以下为4个嫌疑犯的供词:
|
||||
//
|
||||
//
|
||||
//
|
||||
//A说:不是我。
|
||||
//
|
||||
//B说:是C。
|
||||
//
|
||||
//C说:是D。
|
||||
//
|
||||
//D说:C在胡说
|
||||
//
|
||||
//已知3个人说了真话,1个人说的是假话。
|
||||
//
|
||||
//
|
||||
//
|
||||
//现在请根据这些信息,写一个程序来确定到底谁是凶手。
|
||||
|
||||
#include<stdio.h>
|
||||
|
||||
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;
|
||||
}
|
||||
48
code/answear/advanced C/test/01/07 猜名次/test1.c
Normal file
48
code/answear/advanced C/test/01/07 猜名次/test1.c
Normal file
@@ -0,0 +1,48 @@
|
||||
//#### 7. 猜名次
|
||||
//
|
||||
//5位运动员参加了10米台跳水比赛,有人让他们预测比赛结果:
|
||||
//
|
||||
//A选手说:B第二,我第三;
|
||||
//
|
||||
//B选手说:我第二,E第四;
|
||||
//
|
||||
//C选手说:我第一,D第二;
|
||||
//
|
||||
//D选手说:C最后,我第三;
|
||||
//
|
||||
//E选手说:我第四,A第一;
|
||||
//
|
||||
//比赛结束后,每位选手都说对了一半,请编程确定比赛的名次。
|
||||
|
||||
#include<stdio.h>
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user