【题目描述】
输入一行单词序列,相邻单词之间由1个或多个空格间隔,请按照字典序输出这些单词,要求重复的单词只输出一次。(区分大小写)
【输入】
一行单词序列,最少1个单词,最多100个单词,每个单词长度不超过50,单词之间用至少1个空格间隔。数据不含除字母、空格外的其他字符。
【输出】
按字典序输出这些单词,重复的单词只输出一次。
【输入样例】
She wants to go to Peking University to study Chinese
【输出样例】
Chinese
Peking
She
University
go
study
to
wants
#include<bits/stdc++.h>
using namespace std;
string str[1000]; //定义字符串数组
int main()
{
string s; //定义字符串
int cnt=0; //统计单词数
while(cin>>s) //输入单词
{
str[cnt]=s; //将单词存到字符串数组中
cnt++;
}
sort(str,str+cnt); //单词排序 sort定义从小到大排序
cout<<str[0]<<endl;
for(int i=1;i<=cnt;i++)
{
if(str[i]!=str[i-1]) //从第二个单词开始,每次和上一个单词比较是否相同(c++) 在c语言中比较大小应该用strcmp
cout<<str[i]<<endl; //从第二个单词开始,遍历字符串数组输出
}
return 0;
}