题解 | #图片整理#

图片整理

http://www.nowcoder.com/practice/2de4127fda5e46858aa85d254af43941

太简单了我就写了个动态分配内存版本的

#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#define MAX 10
struct Word
{
    char* strarr;
    int sz;
    int max;
};

void Expand(struct Word* pc)//扩容
{
    if (pc->max == pc->sz)
    {
        pc->max += MAX;
        char* ret = (char*)realloc(pc->strarr, pc->max);
        if (NULL != ret)
        {
            pc->strarr = ret;
        }
        else
        {
            perror("realloc");
        }
    }
}
int my_cmp(const void* elem1, const void* elem2)
{
    return strcmp((char*)elem1, (char*)elem2);
}
int main()
{
    struct Word One = {0};
    One.max = MAX;
    One.strarr = (char*)malloc(MAX * sizeof(char));
    if (NULL == One.strarr)
    {
        perror("malloc");
        return 1;
    }
    while (1)
    {
        scanf("%c", One.strarr + One.sz);
        if ('\n' == *(One.strarr + One.sz))
        {
            *(One.strarr + One.sz) = '\0';
            break;
        }
        One.sz++;
        Expand(&One);
    }

    int ret = (int)strlen(One.strarr);
    qsort(One.strarr, strlen(One.strarr), sizeof(char), my_cmp);
    printf("%s", One.strarr);
    return 0;
}
全部评论

相关推荐

每晚夜里独自颤抖:这个在牛客不是老熟人了吗
点赞 评论 收藏
分享
06-28 22:48
已编辑
广东金融学院 Java
小浪_Coding:学院本+这俩项目不是buff叠满了嘛
点赞 评论 收藏
分享
不愿透露姓名的神秘牛友
07-02 18:35
简历上把1个月实习写成了3个月,会进行背调吗?
码农索隆:一个月有一个月的实习经历,三个月有三个月的实习经历
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务