目录
题目描述
输入描述
输出描述
解析
完整代码
描述
输出著名短句"hello world
"。
(注意!要用至少30种算法!)
输入描述
无
输出描述
输出"hello world
"。
用例输入 1
无
用例输出 1
hello world
兄弟们啊,说实话,这题你们不会很正常。
在C++中,即使为了完成一个简单的任务如输出"hello world",我们也可以创意性地融入多种算法。当然,实际应用中不需要这么复杂,但为了满足题目要求,我们可以构造一个涉及多种算法的示例。以下是使用超过三十种算法(包括一些基本和复杂的算法)来输出"hello world"的程序(不完整):
#include <iostream>
#include <vector>
#include <algorithm>
#include <numeric>
#include <string>
#include <iterator>
#include <cctype>
int main() {
// 使用的算法列表
// 1. std::vector初始化
// 2. std::string初始化
// 3. std::toupper
// 4. std::tolower
// 5. std::copy
// 6. std::ostream_iterator
// 7. std::find
// 8. std::find_if
// 9. std::all_of
// 10. std::any_of
// 11. std::none_of
// 12. std::for_each
// 13. std::count
// 14. std::count_if
// 15. std::mismatch
// 16. std::equal
// 17. std::fill
// 18. std::fill_n
// 19. std::generate
// 20. std::generate_n
// 21. std::replace
// 22. std::replace_if
// 23. std::swap
// 24. std::reverse
// 25. std::reverse_copy
// 26. std::rotate
// 27. std::rotate_copy
// 28. std::unique
// 29. std::unique_copy
// 30. std::partition
// 31. std::sort (虽然没直接用,但为了展示可以加入)
std::vector<char> chars;
std::string hello_world = "hello world";
// 1. std::vector初始化 (用 ' ' 填充11个位置)
chars.resize(11, ' ');
// 2. std::string初始化
std::string hw_copy = hello_world;
// 3, 4. std::toupper, std::tolower (示例,不改变字符串)
for (char& c : hw_copy) {
std::toupper(c);
std::tolower(c);
}
// 5. std::copy (将hello_world拷贝到chars)
std::copy(hello_world.begin(), hello_world.end(), chars.begin());
// 6. std::ostream_iterator (输出chars中的字符)
std::copy(chars.begin(), chars.end(), std::ostream_iterator<char>(std::cout));
std::cout << std::endl;
// 7. std::find (查找'h')
auto it = std::find(chars.begin(), chars.end(), 'h');
// 8. std::find_if (查找第一个空格)
auto space_it = std::find_if(chars.begin(), chars.end(), [](char c){ return c == ' '; });
// 9. std::all_of (检查是否都是小写)
bool all_lower = std::all_of(chars.begin(), chars.end(), [](char c){ return std::islower(c); });
// 10. std::any_of (检查是否有'h')
bool has_h = std::any_of(chars.begin(), chars.end(), [](char c){ return c == 'h'; });
// 11. std::none_of (检查是否没有大写字母)
bool no_upper = std::none_of(chars.begin(), chars.end(), [](char c){ return std::isupper(c); });
// 12. std::for_each (打印每个字符)
std::for_each(chars.begin(), chars.end(), [](char c){ std::cout << c; });
std::cout << std::endl;
// 13. std::count (计算'l'的数量)
int l_count = std::count(chars.begin(), chars.end(), 'l');
// 14. std::count_if (计算字母的数量)
int letter_count = std::count_if(chars.begin(), chars.end(), [](char c){ return std::isalpha(c); });
// 15. std::mismatch (比较两个字符串)
auto mismatch_pair = std::mismatch(hello_world.begin(), hello_world.end(), chars.begin());
// 16. std::equal (检查两个字符串是否相等)
bool equal = std::equal(hello_world.begin(), hello_world.end(), chars.
//畏惧了,装不下去了~~~^_^_^_^
不闹了,正常点吧。
#include <bits/stdc++.h>
using namespace std;
int main() {
cout<<"hello world";
return 0;
}
结束了!
对了,忘说了一句话:
要想c++成绩好,就来jiabei小课堂。
还有,点我主页,看我简介,别给那三个人点赞就完了