> str[i]; f_输入n个字符串,将它们按字母由小到大的顺序排列并输出。">
赞
踩
课后习题5.14 输入n个字符串,将它们按字母由小到大的顺序排列并输出。
#include <iostream> using namespace std; int main() { const int n = 5; int i, j; string str[n], temp; cout << "please input strings:" << endl; for (i = 0; i < n; i++) cin >> str[i]; for (i = 0; i < n - 1; i++) { for (j = 0; j < n - i - 1; j++) { if (str[j] > str[j + 1]) { temp = str[j]; str[j] = str[j + 1]; str[j + 1] = temp; } } } cout << endl << "sorted strings:" << endl; for (i = 0; i < n; i++) cout << str[i] << endl; return 0; }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。