c++代码完成 对给定的n1=n=20个国家名。国家名字长度不超过20。按其字母的顺序输出。输入第一行为国家的个数。以下n行为国家的名字。输出行排序后的国名输入样例 3 KoreaChina Japan输出样例China KoreaJapan
以下是一个完整的C++代码实现:
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
int main() {
int n;
cin >> n;
cin.ignore(); // 忽略第一行的换行符
vector<string> countries;
for (int i = 0; i < n; i++) {
string country;
getline(cin, country);
countries.push_back(country);
}
sort(countries.begin(), countries.end());
for (const string& country : countries) {
cout << country << endl;
}
return 0;
}
这个代码使用了vector<string>来保存国家名,然后使用sort函数对这些国家名进行排序,最后按照排序后的顺序输出国名
原文地址: https://www.cveoy.top/t/topic/hZ18 著作权归作者所有。请勿转载和采集!