描述
输入一行单词序列,相邻单词之间由1个或多个空格间隔,请按照字母顺序输出这些单词(即按照字符串中字母的ASCII码排序,区分大小写,当首字母相同时,比较第2个字母,依次类推),要求重复的单词只输出一次。
关于输入
一行单词序列,最少1个单词,最多100个单词,每个单词长度不超过50,单词之间用至少1个空格间隔。
关于输出
按字母顺序输出这些单词,重复的单词只输出一次。
例子输入
She wants to go to Peking University to study Chinese
例子输出
Chinese
Peking
She
University
go
study
to
wants
源码实现
#include <iostream>
#include <iomanip>
#include <string>
#include <vector>
#include <algorithm>
#include <cmath>
using namespace std;
int main()
{
string words[100];
int num = 0;
while (cin >> words[num]) {
bool has = false;
for (int i = 0; i < num; i++) {
if (words[i].compare(words[num]) == 0) {
has = true;
break;
}
}
if (!has) num++;
}
sort(words, words + num);
for (int i = 0; i < num; i++) {
cout << words[i] << endl;
}
return 0;
}