>str[i];_输入n个字符串,将它们按照字母由小到大的顺序排列并输出。(使用选择排序)">
赞
踩
习题 5.14 输入n个字符串,将它们按字母由小到大的顺序排列并输出。
代码:
- #include<iostream>
- using namespace std;
-
- int main()
- {
- const int N = 5;
- int i,j,min_i;
- string str[N],min;
-
-
- cout << "请输入五个字符串:" << endl;
- for (i = 0; i != N; ++i)
- {
- cin >>str[i];
- }
-
-
- for (i = 0; i != N; ++i) //选择排序
- {
- min = str[i];
- min_i = i;
- for (j = i + 1; j != N; ++j)
- {
- if (str[j] < min)
- {
- min = str[j];
- min_i = j;
- }
- }
- str[min_i] = str[i];
- str[i] = min;
- }
-
- cout << "按从小到大顺序排列为:" << endl;
- for (i = 0; i != N; ++i)
- {
- cout << str[i] << endl;
- }
-
-
- return 0;
- }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。