forked from coderit666/CNote
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
1c6c83b
commit a049858
Showing
52 changed files
with
4,591 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
## 计数排序(Counting Sort) | ||
|
||
- 计数排序是一个非基于比较的排序算法,该算法于1954年由 Harold H. Seward 提出。它的优势在于在`对一定范围内的整数排序`时,快于任何比较排序算法。 | ||
- 排序思路: | ||
|
||
+ 1.找出待排序数组最大值 | ||
+ 2.定义一个索引最大值为待排序数组最大值的数组 | ||
+ 3.遍历待排序数组, 将待排序数组遍历到的值作新数组索引 | ||
+ 4.在新数组对应索引存储值原有基础上+1 | ||
![](https://img-blog.csdnimg.cn/img_convert/eaed8e0890b2a50353ec24850941e789.png) | ||
|
||
|
||
- 简单代码实现: | ||
|
||
```c | ||
int main() | ||
{ | ||
// 待排序数组 | ||
int nums[5] = {3, 1, 2, 0, 3}; | ||
// 用于排序数组 | ||
int newNums[4] = {0}; | ||
// 计算待排序数组长度 | ||
int len = sizeof(nums) / sizeof(nums[0]); | ||
// 遍历待排序数组 | ||
for(int i = 0; i < len; i++){ | ||
// 取出待排序数组当前值 | ||
int index = nums[i]; | ||
// 将待排序数组当前值作为排序数组索引 | ||
// 将用于排序数组对应索引原有值+1 | ||
newNums[index] = newNums[index] +1; | ||
} | ||
|
||
// 计算待排序数组长度 | ||
int len2 = sizeof(newNums) / sizeof(newNums[0]); | ||
// 输出排序数组索引, 就是排序之后结果 | ||
for(int i = 0; i < len2; i++){ | ||
for(int j = 0; j < newNums[i]; j++){ | ||
printf("%i\n", i); | ||
} | ||
} | ||
/* | ||
// 计算待排序数组长度 | ||
int len2 = sizeof(newNums) / sizeof(newNums[0]); | ||
// 还原排序结果到待排序数组 | ||
for(int i = 0; i < len2; i++){ | ||
int index = 0; | ||
for(int i = 0; i < len; i++){ | ||
for(int j = 0; j < newNums[i]; j++){ | ||
nums[index++] = i; | ||
} | ||
} | ||
} | ||
*/ | ||
return 0; | ||
} | ||
``` | ||
|
||
## | ||
|
||
最后,如果有任何疑问,请加微信 **leader_fengy** 拉你进学习交流群。 | ||
|
||
开源不易,码字不易,如果觉得有价值,欢迎分享支持。 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,113 @@ | ||
## 二维数组与函数 | ||
|
||
- 值传递 | ||
|
||
```c | ||
#include <stdio.h> | ||
|
||
// 和一位数组一样, 只看形参是基本类型还是数组类型 | ||
// 如果是基本类型在函数中修改形参不会影响实参 | ||
void change(char ch){ | ||
ch = 'n'; | ||
} | ||
int main() | ||
{ | ||
char cs[2][3] = { | ||
{'a', 'b', 'c'}, | ||
{'d', 'e', 'f'} | ||
}; | ||
printf("cs[0][0] = %c\n", cs[0][0]); // a | ||
change(cs[0][0]); | ||
printf("cs[0][0] = %c\n", cs[0][0]); // a | ||
return 0; | ||
} | ||
``` | ||
- 地址传递 | ||
```c | ||
#include <stdio.h> | ||
// 和一位数组一样, 只看形参是基本类型还是数组类型 | ||
// 如果是数组类型在函数中修改形参会影响实参 | ||
void change(char ch[]){ | ||
ch[0] = 'n'; | ||
} | ||
int main() | ||
{ | ||
char cs[2][3] = { | ||
{'a', 'b', 'c'}, | ||
{'d', 'e', 'f'} | ||
}; | ||
printf("cs[0][0] = %c\n", cs[0][0]); // a | ||
change(cs[0]); | ||
printf("cs[0][0] = %c\n", cs[0][0]); // n | ||
return 0; | ||
} | ||
``` | ||
|
||
```c | ||
#include <stdio.h> | ||
|
||
// 和一位数组一样, 只看形参是基本类型还是数组类型 | ||
// 如果是数组类型在函数中修改形参会影响实参 | ||
void change(char ch[][3]){ | ||
ch[0][0] = 'n'; | ||
} | ||
int main() | ||
{ | ||
char cs[2][3] = { | ||
{'a', 'b', 'c'}, | ||
{'d', 'e', 'f'} | ||
}; | ||
printf("cs[0][0] = %c\n", cs[0][0]); // a | ||
change(cs); | ||
printf("cs[0][0] = %c\n", cs[0][0]); // n | ||
return 0; | ||
} | ||
``` | ||
- 形参错误写法 | ||
```c | ||
void test(char cs[2][]) // 错误写法 | ||
{ | ||
printf("我被执行了\n"); | ||
} | ||
void test(char cs[2][3]) // 正确写法 | ||
{ | ||
printf("我被执行了\n"); | ||
} | ||
void test(char cs[][3]) // 正确写法 | ||
{ | ||
printf("我被执行了\n"); | ||
} | ||
``` | ||
|
||
- 二维数组作为函数参数,在被调函数中不能获得其有多少行,需要通过参数传入 | ||
|
||
```c | ||
void test(char cs[2][3]) | ||
{ | ||
int row = sizeof(cs); // 输出4或8 | ||
printf("row = %zu\n", row); | ||
} | ||
``` | ||
- 二维数组作为函数参数,在被调函数中可以计算出二维数组有多少列 | ||
```c | ||
void test(char cs[2][3]) | ||
{ | ||
size_t col = sizeof(cs[0]); // 输出3 | ||
printf("col = %zd\n", col); | ||
} | ||
``` | ||
|
||
## | ||
|
||
最后,如果有任何疑问,请加微信 **leader_fengy** 拉你进学习交流群。 | ||
|
||
开源不易,码字不易,如果觉得有价值,欢迎分享支持。 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
## 作业 | ||
|
||
- 玩家通过键盘录入 w,s,a,d控制小人向不同方向移动,其中w代表向上移动,s代表向 下移动,a代表向左移动,d 代表向右移动,当小人移动到出口位置,玩家胜利 | ||
|
||
- 思路: | ||
- 1.定义二维数组存放地图 | ||
|
||
```c | ||
###### | ||
#O # | ||
# ## # | ||
# # # | ||
## # | ||
###### | ||
``` | ||
|
||
- 2.规定地图的方向 | ||
![](https://img-blog.csdnimg.cn/img_convert/7331a7700b09543cef73fa0bbddc3c16.png) | ||
- 3.编写程序控制方向 | ||
|
||
+ 当输入w或者W, 小人向上移动. x-1 | ||
+ 当输入s 或者S, 小人向下. x+1 | ||
+ 当输入a或者A, 小人向左. y-1 | ||
+ 当输入d或者D, 小人向右. y+1 | ||
|
||
- 4.移动小人 | ||
|
||
+ 用变量记录小人当前的位置 | ||
+ 1)如果小人将要移动的位置是墙,则无法移动 | ||
+ 2)如果小人将要移动的位置是路,则可以移动 | ||
|
||
- 5.判断是否走出迷宫 | ||
|
||
## | ||
|
||
最后,如果有任何疑问,请加微信 **leader_fengy** 拉你进学习交流群。 | ||
|
||
开源不易,码字不易,如果觉得有价值,欢迎分享支持。 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
## 字符串的基本概念 | ||
|
||
- 字符串是位于双引号中的字符序列 | ||
|
||
+ 在内存中以“\0”结束,所占字节比实际多一个 | ||
![](https://img-blog.csdnimg.cn/img_convert/e0f0831ff1b110569a7e34440ab62e9e.png) | ||
|
||
## 字符串的初始化 | ||
|
||
- 在C语言中没有专门的字符串变量,通常用一个字符数组来存放一个字符串。 | ||
- 当把一个字符串存入一个数组时,会把结束符‘\0’存入数组,并以此作为该字符串是否结束的标志。 | ||
- 有了‘\0’标志后,就不必再用字符数组 的长度来判断字符串的长度了 | ||
- 初始化 | ||
|
||
```c | ||
char name[9] = "lnj"; //在内存中以“\0”结束, \0ASCII码值是0 | ||
char name1[9] = {'l','n','j','\0'}; | ||
char name2[9] = {'l','n','j',0}; | ||
// 当数组元素个数大于存储字符内容时, 未被初始化的部分默认值是0, 所以下面也可以看做是一个字符串 | ||
char name3[9] = {'l','n','j'}; | ||
``` | ||
- 错误的初始化方式 | ||
```c | ||
//省略元素个数时, 不能省略末尾的\n | ||
// 不正确地写法,结尾没有\0 ,只是普通的字符数组 | ||
char name4[] = {'l','n','j'}; | ||
// "中间不能包含\0", 因为\0是字符串的结束标志 | ||
// \0的作用:字符串结束的标志 | ||
char name[] = "c\0ool"; | ||
printf("name = %s\n",name); | ||
输出结果: c | ||
``` | ||
|
||
--- | ||
|
||
## 字符串输出 | ||
|
||
- 如果字符数组中存储的是一个字符串, 那么字符数组的输入输出将变得简单方便。 | ||
|
||
+ 不必使用循环语句逐个地输入输出每个字符 | ||
+ 可以使用printf函数和scanf函数一次性输出输入一个字符数组中的字符串 | ||
|
||
- 使用的格式字符串为“%s”,表示输入、输出的是一个字符串 字符串的输出 | ||
|
||
--- | ||
|
||
- **输出** | ||
|
||
+ %s的本质就是根据传入的name的地址逐个去取数组中的元素然后输出,直到遇到\0位置 | ||
|
||
```c | ||
char chs[] = "lnj"; | ||
printf("%s\n", chs); | ||
``` | ||
- 注意点: | ||
+ \0引发的脏读问题 | ||
```c | ||
char name[] = {'c', 'o', 'o', 'l' , '\0'}; | ||
char name2[] = {'l', 'n', 'j'}; | ||
printf("name2 = %s\n", name2); // 输出结果: lnjcool | ||
``` | ||
|
||
--- | ||
|
||
- **输入** | ||
|
||
```c | ||
char ch[10]; | ||
scanf("%s",ch); | ||
``` | ||
- 注意点: | ||
+ 对一个字符串数组, 如果不做初始化赋值, 必须指定数组长度 | ||
+ ch最多存放由9个字符构成的字符串,其中最后一个字符的位置要留给字符串的结尾标示‘\0’ | ||
+ 当用scanf函数输入字符串时,字符串中不能含有空格,否则将以空格作为串的结束符 | ||
最后,如果有任何疑问,请加微信 **leader_fengy** 拉你进学习交流群。 | ||
开源不易,码字不易,如果觉得有价值,欢迎分享支持。 |
Oops, something went wrong.