【1185】单词排序

【问题描述】
       输入一行单词序列,相邻单词之间由1个或多个空格间隔,请按照字典序输出这些单词,要求重复的单词只输出一次。(区分大小写)。
【输入】
       一行单词序列,最少1个单词,最多100个单词,每个单词长度不超过50,单词之间用至少1个空格间隔。数据不含除字母、空格外的其他字符。。
【输出】
       按字典序输出这些单词,重复的单词只输出一次。。
【输入样例】
       She wants to go to Peking University to study Chinese
【输出样例】
       Chinese
       Peking
       She
       University
       go
       study
       to
       wants
【参考程序】

#include <cstdio> 
#include <iostream>
#include <cstring>
using namespace std;

int main() {
	int cnt = 0; 
	string ss, s[101];
	while (cin >> ss) {						// 用于反复读入 (回车、ctrl+z、回车结束循环输入) 
		s[++cnt] = ss; 
	}
	
	for (int i=1; i<cnt; i++) {				// 字符串从小到大,选择排序 
		for (int j=i+1; j<=cnt; j++) {
			if (s[i] > s[j]) {
				swap(s[i], s[j]);
			}
		}
	}
	
	for (int i=1; i<=cnt; i++) {
		if (s[i-1] != s[i]) {				// 不重复第输出 
			cout << s[i] << endl;
		}
	} 
		
	return 0;
}
发布了66 篇原创文章 · 获赞 0 · 访问量 1218

猜你喜欢

转载自blog.csdn.net/developer_zhb/article/details/105466686