题解 | #图片整理#
图片整理
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;
}