一开始看难度是一,直接跳过不做了,结果。。。
大神博客地址http://blog.youkuaiyun.com/c_duoduo/article/details/51449537 thank you~
对字符串进行排序,要看两个字符串怎样组成的新字符串来排序。
#include<iostream>
#include<stdio.h>
#include<math.h>
#include<string.h>
#include<vector>
#include<list>
#include<algorithm>
using namespace std;
struct node{
char a[60];
bool operator < (const node& t)const{ //组成两个新的字符串,进行排序
char c1[110],c2[110]; //注意开的字符串大小,是两个要求的字符串
strcpy(c1,a);
strcat(c1,t.a);
strcpy(c2,t.a);
strcat(c2,a);
return strcmp(c1,c2)>=0?true:false;//这里降序排列了,注意字符串排列要返回bool值
}
}a[200]; //字符串200够用了
int main(){
int T;
scanf("%d",&T);
getchar();
getchar();
while(T--){
int n = 0;
while(1){
if(*gets(a[n].a)!=NULL){ //学习了,当读入'\n'则gets返回指针指向NULL
n++;
}
else break;
}
sort(a,a+n);
for(int i=0;i<n;i++){
printf("%s",a[i].a);
}
printf("\n");
for(int i=n-1;i>=0;i--){
printf("%s",a[i].a);
}
printf("\n");
}
return 0;
}