F - Andy's First Dictionary
题意:对一段文章中单单词进行排序后输出。按字典排序。
题解:1.将单个单词从从文章中提取出来存到字符数组中。2.对单词进行排序。
提取单词的时候一个字符一个字符提取,遇到非单词字符则转到下一个单词提取。
排序用qsort快速排序,有个cmp函数是指排序方式。
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
using namespace std;
char word[1000001][205];
int cmp(const void * s1,const void * s2)
{
return strcmp((char *)s1, (char *)s2);
}
int main()
{
char c;
int i = 0,j = 0;
while((c = getchar()) != EOF)
{
if(c <= 'Z' && c >= 'A')
{
word[i][j++] = c + 32;
}
else if(c <= 'z' && c >= 'a')
{
word[i][j++] = c;
}
else if(j)
{
word[i++][j] = '\n';
j = 0;
}
}
qsort(word,i,sizeof(word[0]),cmp);
for(int x = 0;x < i;x++)
{
if(strcmp(word[x - 1],word[x]))
printf("%s",word[x]);
}
return 0;
}