其他分享
首页 > 其他分享> > 字符串排序.qsort

字符串排序.qsort

作者:互联网

题目描述:实现对输入的多个字符串进行排序。


#include<stdio.h>
#include<stdlib.h>
#include<string.h>
int shengxu(char *s1, char *s2);//快速排序
int main(){
    int i,n;
    char s[100][85];
	scanf("%d",&n);
    for(i = 0; i < n; i++)
    scanf("%s", s[i]);               //读入n个字符串
    qsort(s, n, sizeof(s) / 100,shengxu ); //sizeof(s) / 100 为每个一维数组的大小
    printf("排序后为\n");
    for(i = 0; i < n; i++)
        printf("%s\n", s[i]);
    return 0;
}
 
int shengxu(char *s1, char *s2){           //自写比较函数
    return strcmp(s1, s2);//字符串升序
	//return strcmp(s2, s1);//字符串降序
}
//解题思路:用strcmp比较字符串大小,然后进行快速排序
//错误原因:sizeof(s)/100;
 

看无数个思路,还是这个好。

标签:int,s2,s1,qsort,char,字符串,100,排序
来源: https://blog.csdn.net/m0_60109224/article/details/121986223