1. 首先按字符串长度进行排序,对长度相同的字符串,按字母顺序进行排序。
2. 直接按字母顺序排序,把长度不同的串都按相同长度处理。
#include<stdio.h>
#include<string.h>
#include<iostream>
#include<algorithm>
using namespace std;
struct date
{
char b[1000];
};
struct date a[1000];
bool cmp(date x,date y)
{
if(strlen(x.b)>strlen(y.b))
return 0;
else if(strlen(x.b)==strlen(y.b))
{
if (strcmp(y.b,x.b)>0)
return 1;
else return 0;
}
return 1;
}
bool cmp1(date x,date y)
{
if(strcmp(x.b,y.b)>0)
return 0;
else return 1;
}
int main()
{
int n,i=0;
scanf("%d",&n);
for(i=0;i<n;i++)
{
scanf("%s",a[i].b);
}
sort(a,a+n,cmp);
for(i=0;i<n;i++)
{
if(i==0)
printf("%s",a[i].b);
else printf(" %s",a[i].b);
}
printf("\n");
sort(a,a+n,cmp1);
for(i=0;i<n;i++)
{
if(i==0)
printf("%s",a[i].b);
else printf(" %s",a[i].b);
}
printf("\n");
return 0;
}