题目描述:实现对输入的多个字符串进行排序。
#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;
}