本文共 716 字,大约阅读时间需要 2 分钟。
方法一:冒泡排序
#include#includeusing namespace std; int main() {const int n = 4;string str[n];string t;int i = 0;for (i = 0; i < n; i++) {cin >> str[i];}for (int i = 0; i < n - 1; i++) {for (int j = 0; j < n - 1 - i; j++) {if (str[j] > str[j + 1]) {t = str[j];str[j] = str[j + 1];str[j + 1] = t;}}}for (int i = 0; i < n; i++) {cout << str[i] << endl;}return 0;}
方法二:选择排序
#include#includeusing namespace std; int main() {const int n = 4;string str[n];string t;int i = 0;for (i = 0; i < n; i++) {cin >> str[i];}for (int i = 0; i < n - 1; i++) {for (int j = i + 1; j < n; j++) {if (str[i] > str[j]) {t = str[j];str[j] = str[i];str[i] = t;}}}for (int i = 0; i < n; i++) {cout << str[i] << endl;}return 0;}
转载地址:http://qnxwk.baihongyu.com/