C++题目51: 对英文名字进行排序 输入5个英文人名,对人名进行排序输出(字典序,升序)。 【输入形式】 5个英文人名 【输出形式】 排序后的人名 【样例输入】 Bob Candy Andy Elle David 【样例输出】 Andy Bob Candy David Elle
时间: 2023-12-01 20:03:40 浏览: 121
好的,这是一个简单的排序问题,我可以帮你写一个C++程序解决这个问题。以下是代码实现:
```c++
#include <iostream>
#include <algorithm>
#include <string>
using namespace std;
int main()
{
string names[5]; // 存储5个英文人名
cout << "请输入5个英文人名:" << endl;
for(int i = 0; i < 5; i++)
{
cin >> names[i];
}
sort(names, names + 5); // 对人名进行排序
cout << "排序后的人名为:" << endl;
for(int i = 0; i < 5; i++)
{
cout << names[i] << " ";
}
cout << endl;
return 0;
}
```
输入样例:
```
Bob Candy Andy Elle David
```
输出样例:
```
排序后的人名为:
Andy Bob Candy David Elle
```
希望能够帮到你!
相关问题
C++题目51: 对英文名字进行排序 输入5个英文人名,对人名进行排序输出(字典序,升序)。 【输入形式】 5个英文人名 【输出形式】 排序后的人名
好的,这是一个简单的排序问题,可以使用C++中的sort函数进行排序。以下是代码实现:
```c++
#include <iostream>
#include <string>
#include <algorithm>
using namespace std;
int main()
{
string names[5];
cout << "请输入5个英文人名:" << endl;
for(int i=0; i<5; i++)
{
cin >> names[i];
}
sort(names, names+5);
cout << "排序后的人名为:" << endl;
for(int i=0; i<5; i++)
{
cout << names[i] << endl;
}
return 0;
}
```
运行结果:
```
请输入5个英文人名:
Tom Jerry Alice Bob David
排序后的人名为:
Alice
Bob
David
Jerry
Tom
```
c++对英文名字进行排序 输入5个英文人名,对人名进行排序输出(字典序,升序)。
好的,以下是实现该功能的 C++ 代码:
```cpp
#include <iostream>
#include <algorithm>
#include <string>
using namespace std;
int main() {
const int n = 5;
string names[n];
// 输入人名
cout << "请输入" << n << "个英文人名:" << endl;
for (int i = 0; i < n; i++) {
getline(cin, names[i]);
}
// 按字典序排序
sort(names, names + n);
// 输出排序结果
cout << "排序后的结果为:" << endl;
for (int i = 0; i < n; i++) {
cout << names[i] << endl;
}
return 0;
}
```
代码中,我们首先定义了一个长度为 5 的字符串数组 `names` 来存储输入的人名。然后通过循环和 `getline` 函数来实现输入,`sort` 函数实现按字典序排序,最后通过循环输出排序结果。
相关推荐
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)