分享

东华大学OJ基础79题

 印度阿三17 2020-04-06

这道题感觉是我做了几十道题之后融合的结果。感觉很不错。

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

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多