字符串排序.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;
 
#include<iostream>
#include<stdlib.h>
using namespace std;
int desc(const void *a,const void *b)
{
	return *(int *)a-*(int *)b;
}
int main()
{
	int i=0,j,a[100];
	while(scanf("%d",&a[i])!=EOF)
	{
		i++;
	}
	printf("%d\n",i);
	qsort(a,i,sizeof(a[0]),desc);
	for(j=0;j<i;j++)
	{
		cout<<a[j];
	}
	return 0;
}

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值