P1036 单词排序
描述
输入一行单词序列,相邻单词之间由1个或多个空格间隔,请按照从小到大的顺序输出这些单词,要求重复的单词只输出一次。(区分大小写)
格式
输入格式
一行单词序列,最少1个单词,最多100个单词,每个单词长度不超过50,单词之间用至少1个空格间隔。数据不含除字母、空格外的其他字符。
输出格式
按字典序输出这些单词,重复的单词只输出一次。
样例
输入样例
She wants to go to Peking University to study Chinese
输出样例
Chinese
Peking
She
University
go
study
to
wants
限制
时间限制: 1000 ms
内存限制: 65536 KB
我完全用的sort函数和unique函数,没有一点技术含量,就很残酷
#include<stdio.h>
#include<iostream>
#include<string.h>
#include<algorithm>
using namespace std;
int main()
{
string words[105];
int n=0;
while(cin>>words[n++]);
sort(words,words+n);
n=unique(words,words+n)-words;
for(int i=0;i<n;i++)
{
cout<<words[i]<<endl;
}
return 0;
}
第二种在输入的时候就加以判断,就不用后面的unique了
#include<stdio.h>
#include<iostream>
#include<string.h>
#include<algorithm>
using namespace std;
int main()
{
string str[110];
int n=0,temp;
while(cin>>str[n])
{
temp=0;
for(int i=0; i<n; i++)
{
if(str[i].compare(str[n])==0)temp=1;
}
if(temp==0)n++;
}
sort(str,str+n);
for(int i=0; i<n; i++)
cout<<str[i]<<endl;
return 0;
}