/*
014-字串的连接最长路径查找
题目描述
给定n个字符串,请对n个字符串按照字典序排列。
输入描述:
输入第一行为一个正整数n(1≤n≤1000),下面n行为n个字符串(字符串长度≤100),字符串中只含有大小写字母。
输出描述:
数据输出n行,输出结果为按照字典序排列的字符串。
输入例子:
9
cap
to
cat
card
two
too
up
boat
boot
输出例子:
boat
boot
cap
card
cat
to
too
two
up
*/
//c语言版
#include <stdio.h>
#include <string.h>
#define N 1000
#define M 100
void sortStr(char (*str) [M],int n);
int main(void)
{
char str[N][M];//处理二维字符串数组
int n,i;
while(scanf("%d",&n) == 1)
{
while(getchar() != ‘\n‘) //读取前处理
continue;
for(i = 0;i < n;i++)
{
gets(str[i]);//读取一个字符串
}
sortStr(str,n);
for(i = 0;i < n;i++)
{
puts(str[i]);
}
}
return 0;
}
void sortStr(char (*str) [M],int n)
{
int i,j;
char min[M];
for(i = 0;i < n - 1;i++)
for(j = i + 1;j < n;j++)
{
if(strcmp(str[i],str[j]) > 0)
{
strcpy(min,str[j]);
strcpy(str[j],str[i]);
strcpy(str[i],min);
}
}
}本文出自 “仗剑走天涯” 博客,请务必保留此出处http://yutianheji.blog.51cto.com/9580523/1829149
原文地址:http://yutianheji.blog.51cto.com/9580523/1829149