题解 | #字符串排序#
字符串排序
http://www.nowcoder.com/practice/5af18ba2eb45443aa91a11e848aa6723
//基础的冒泡排序,没啥好说的
#include "stdio.h"
#include "string.h"#include "stdlib.h"
#define uint8_t unsigned char
#define int8_t signed char
#define uint16_t unsigned short
#define int16_t signed short
typedef struct
{
char string[101];
}STRING_T;
void string_sort(STRING_T* str,uint16_t len)
{
STRING_T temp_str = {};
for(uint16_t i=0;i<len-1;i++)
for(uint16_t j=0;j<len-i-1;j++)
{
if(strcmp(str[j].string, str[j+1].string)>0)
{
memcpy(&temp_str, &str[j], sizeof(STRING_T));
memcpy(&str[j], &str[j+1], sizeof(STRING_T));
memcpy(&str[j+1], &temp_str, sizeof(STRING_T));
}
}
for(uint16_t i=0;i<len;i++)
printf("%s\n",str[i].string);
}
int main()
{
uint16_t num = 0;
while(scanf("%d",&num)!=EOF)
{
STRING_T* str = (STRING_T*)malloc(num*sizeof(STRING_T));
for(uint16_t i=0;i<num;i++)
scanf("%s",str[i].string);
string_sort(str,num);
free(str);
}
}