这道题感觉是我做了几十道题之后融合的结果。感觉很不错。 1、使用了两个排序:快排和冒泡 2、使用了两个字符串函数:一个拷贝函数strcpy和一个比较函数strcmp 3、大量使用了指针。之前看csdn上有位博主是使用malloc进行字符串的拆解,个人认为很直观,但是使用malloc用空间换取时间不如指针看的有水平,而且她使用完malloc没有释放,(哈哈哈) 题目:
代码: #include <stdio.h>#include <string.h>char str[11]; char str_copy[11]; char *str_temp[11];void selectSort(int len) { int i, j, min; char temp; memcpy(str_copy, str, len); for(i = 0; i < len; i ){ for(j = min = i; j < len; j ){ if(str_copy[j] < str_copy[min]){ min = j; } } temp = str_copy[i]; str_copy[i] = str_copy[min]; str_copy[min] = temp; }}int findSub(char s, int len) { int i; int j = 0; for(i = 0; i < len; i ){ if(str[i] == s){ str_temp[j ] = &str[i]; //return &str[i]; } } return j; } void bubbleSort(int len) { int i, j; char *temp; if(len < 2){ return; } for(i = 0; i < len-1; i ){ for(j = 0; j < len-1-i; j ){ if(strcmp(str_temp[j], str_temp[j 1]) > 0){ temp = str_temp[j]; str_temp[j] = str_temp[j 1]; str_temp[j 1] = temp; } } } } int main() { int len, i, j, count; char *min ; while(gets(str)){ len = strlen(str); selectSort(len); for(i = 0; i < len; ){ //返回这个字符的所有地址,然后使用字符串比较的结果得到所有字符串再输出 count = findSub(str_copy[i], len); //冒泡排序 bubbleSort(count); for(j = 0; j < count; j ){ printf("%s\n", str_temp[j]); } i = count; } } return 0; }来源:https://www./content-4-673451.html |
|