Part 1
一、二分查找练习
①利用数组
// 练习:使用二分查找,在一组有序元素中查找数据项 // 形参是数组,实参是数组名 #include <stdio.h> const int N=5; int binarySearch(int x[], int n, int item); int main() { int a[N]={1,3,9,16,21}; int i,index, key; printf("数组a中的数据:\n"); for(i=0;i<N;i++) printf("%d ",a[i]); printf("\n"); printf("输入待查找的数据项: "); scanf_s("%d", &key); // 调用函数binarySearch()在数组a中查找指定数据项item,并返回查找结果给index // 补足代码① index = binarySearch(a,N,key); if(index>=0) printf("%d在数组中,下标为%d\n", key, index); else printf("%d不在数组中\n", key); return 0; } //函数功能描述: //使用二分查找算法在数组x中查找特定值item,数组x大小为n // 如果找到,返回其下标 // 如果没找到,返回-1 int binarySearch(int x[], int n, int item) { int low, high, mid; low = 0; high = n-1; while(low <= high) { mid = (low+high)/2; if (item == x[mid]) return mid; else if(/*补足代码②*/item < x[mid]) high = mid - 1; else low = mid + 1; } return -1; }
②利用指针
// 练习:使用二分查找,在一组有序元素中查找数据项 // 形参是指针变量,实参是数组名 #include <stdio.h> const int N=5; int binarySearch(int *x, int n, int item); int main() { int a[N]={1,3,9,16,21}; int i,index, key; printf("数组a中的数据:\n"); for(i=0;i<N;i++) printf("%d ",a[i]); printf("\n"); printf("输入待查找的数据项: "); scanf_s("%d", &key); // 调用函数binarySearch()在数组a中查找指定数据项item,并返回查找结果 // 补足代码① index = binarySearch(a, N, key); if(index>=0) printf("%d在数组中,下标为%d\n", key, index); else printf("%d不在数组中\n", key); return 0; } //函数功能描述: //使用二分查找算法在x指向的数据项开始的n个数据中,查找item // 如果找到,返回其位置 // 如果没找到,返回-1 int binarySearch(int *x, int n, int item) { int low, high, mid; low = 0; high = n-1; while(low <= high) { mid = (low+high)/2; if (item == *(x+mid)) return mid; else if(/*补足代码②*/item < *(x + mid)) high = mid - 1; else low = mid + 1; } return -1; }
Part 2
二、选择排序法
①对一组数据由小到大排序
选择法排序的具体排序过程描述:
设有n个数据项存放在数组a中。
// 示例: 使用选择法排序对一组整数由小到大排序 #include <stdio.h> const int N=5; void selectSort(int [], int); // 函数声明(函数声明中可以省略变量名、数组名) void input(int [], int); void output(int [], int); int main() { int a[N]; printf("输入%d个整数\n", N); input(a, N); printf("排序前的数据:\n"); output(a,N); selectSort(a,N); // 调用selectSort()对数组a中的N个元素排序 printf("排序后的数据:\n"); output(a, N); return 0; } // 函数定义 // 函数功能描述:输入n个整数到数组a中 void input(int a[], int n) { int i; for(i=0; i<n; i++) scanf_s("%d", &a[i]); } // 函数定义 // 函数功能描述:输出数组a中的n个整数 void output(int a[], int n) { int i; for(i=0; i<n; i++) printf("%d ", a[i]); printf("\n"); } // 函数定义 // 函数功能描述:使用选择法对数组a中的n个整数由小到大排序 void selectSort(int a[], int n) { int i, j, k, temp; for(i=0; i<n-1; i++) { k = i; // k用于记录当前最小元素的下标 for(j=i+1; j<n; j++) if (a[j] < a[k]) k = j; // 如果a[j]比当前最小元素还要小,就更新k,确保它总是存放最小元素的下标 if(k != i) { // 找到最小元素后,交换a[i]和a[k] temp = a[i]; a[i] = a[k]; a[k] = temp; } }
②对一字符串按字典序排序
// 练习:使用选择法对字符串按字典序排序 #include <stdio.h> #include <string.h> void selectSort(char str[][20], int n ); // 函数声明,形参str是二维数组名 int main() { char name[][20] = {"John", "Alex", "Joseph", "Candy", "Geoge"}; int i; printf("输出初始名单:\n"); for(i=0; i<5; i++) printf("%s\n", name[i]); selectSort(name, 5); // 调用选择法对name数组中的字符串排序 printf("按字典序输出名单:\n"); for(i=0; i<5; i++) printf("%s\n", name[i]); return 0; } // 函数定义 // 函数功能描述:使用选择法对二维数组str中的n个字符串按字典序排序 void selectSort(char str[][20], int n) { // 补足代码 int i, j, k; char temp[20]; for (i = 0; i < n-1; i++) { k = i; for (j = i+1; j<n; j++) if (strcmp(str[j], str[k]) < 0) k = j; if (k != i) { strcpy_s(temp, str[i]); strcpy_s(str[i], str[k]); strcpy_s(str[k], temp); } } }
Part 3
三、用指针处理字符串
①假定输入的字符串中只包含字母和*,例如字符串****A*BC*DEF*G*******
编写子函数 delPre?xStar(),删除字符串中所有前导*删除,中间的和后面的*不删除。
即删除后,字符串的内容应当是 A*BC*DEF*G*******
// 函数定义 // 函数功能描述 // 删除字符数组s中前导* void delPrefixStar(char s[]) { char *target, *source; // 从字符串开始找到不是*的位置 source = s; while(*source == ‘*‘) source++; // 从这个位置开始将余下的字符前移 target = s; while( *target++ = *source++); }
②假定输入的字符串中只包含字母和*,例如字符串****A*BC*DEF*G*******
编写子函数 delStarButPre?x(),除了前导*之外,删除其它*
即删除后,字符串的内容应当是****ABCDEFG
// 函数定义 // 函数功能描述 // 删除字符数组s中除了前导*以外的所有*(即删除字符串中间和末尾出现的*) void delStarButPrefix(char s[]) { int i=0; // i用于记录字符在字符数组s中的下标 char *p = s; // 跳过前导*,i记录字符在字符数组s中的下标,p记录首个非*字符的位置 while(*p && *p == ‘*‘) { p++; i++; } // 从p指向的字符开始,把遇到的*删除 while(*p) { if(*p != ‘*‘) { s[i] = *p; i++; } p++; } s[i] = ‘\0‘; // 思考:这一步这样写的原因 }
简单分析:从字符串中两个两个的进行是否都为*的判断,以此找出不是*的字符,并从该该位置开始,转为逐个判断是否为*,如果不是*,就把字符复制保留在字符数组s中,是则继续移动指针对下一个进行判断。
思考答案:
’\0’是判定字符数组结束的标识,表示这串字符到结尾了
注意:在字符数组中’\0’是占一个位置的!
例如 定义char c[6]=“hello”,而在内存中字符数组 c 则是"hello\0"
即’\0’在数组中占有空间却不被我们看到
③假定输入的字符串中只包含字母和*,例如字符串****A*BC*DEF*G*******
编写子函数 delMiddleStar(),除了前导*和尾部*之外,删除中间出现的所有*
即删除后,字符串内容应当是 ****ABCDEFG*******
// 函数定义 // 函数功能描述 // 对字符数组s中存放的字符串,删除中间出现的* void delMiddleStar(char s[]) { int i=0; char *tail, *head, *p; // 找到末尾第一个非*字符的位置 tail = s; while(*tail) tail++; tail--; while(*tail == ‘*‘) tail--; // 找到开头第一个非*字符的位置 head = s; while(*head == ‘*‘) head++; // 把中间出现的*去掉 p = s; while(p<=head) { // 思考这里条件表达式为什么这样写,这个循环的功能? s[i] = *p; p++; i++; } while(p<tail) { if(*p != ‘*‘) { s[i] = *p; i++; } p++; } while(*p) { s[i] = *p; i++; p++; } s[i] = ‘\0‘; // 思考这里为什么要这样做 }
简单分析:分别找到尾部第一个非*字符,和首部第一个非*字符的位置。然后分三段处理。中间的一段,删除*。
思考答案:
Part 4
1、字符串比较函数
一般形式为strcmp(字符串1,字符串2)
比较规则:
对两个字符串自左至右逐个字符相比(按ASCII码值大小比较),直到出现不同的字符或遇到‘\0’为止。
如果两个字符串都由英文字母组成,则有一个简单的规律:在英文字典中位置在后面的为“大”,还要特别注意:小写字母比大写字母“大”。
返回值:
(1)字符串1=字符串2,返回0
(2)字符串1>字符串2,返回一个正整数
(3)字符串1<字符串2,返回一个负整数。
2、字符串复制函数
一般形式为strcpy(str1,str2)
str1,str2是两个字符串数组的数组名
这句语句就是把str2这个字符串复制给str1
理解起来的话就等于是str1[]=str2[];
3、指针和数组要掌握的真的挺多的啊,而且运用特别灵活,这个部分得好好学学( ̄︶ ̄)↗
感谢阅读!!!
原文:https://www.cnblogs.com/gundongtiao/p/10926672.html