kutuzzzov

Задачи на алгоритмы и лябда-функции

Nov 26th, 2025
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 11.46 KB | None | 0 0
  1. #### **1. Найти сумму всех четных чисел в векторе**
  2.  
  3. ```cpp
  4. #include <iostream>
  5. #include <vector>
  6. #include <numeric>
  7.  
  8. int sumEvenNumbers(const std::vector<int>& numbers) {
  9.     // напишите ваше решение
  10. }
  11.  
  12. int main() {
  13.     std::vector<int> numbers = {1, 2, 3, 4, 5, 6};
  14.     int result = sumEvenNumbers(numbers);
  15.     std::cout << "Сумма четных чисел: " << result << std::endl; // Ожидаемый результат: 12
  16.     return 0;
  17. }
  18. ```
  19.  
  20. ---
  21.  
  22. #### **2. Найти максимальный элемент в векторе**
  23.  
  24. ```cpp
  25. #include <iostream>
  26. #include <vector>
  27. #include <algorithm>
  28.  
  29. int findMaxElement(const std::vector<int>& numbers) {
  30.     // напишите ваше решение
  31. }
  32.  
  33. int main() {
  34.     std::vector<int> numbers = {10, 20, 5, 7, 30};
  35.     int result = findMaxElement(numbers);
  36.     std::cout << "Максимальный элемент: " << result << std::endl; // Ожидаемый результат: 30
  37.     return 0;
  38. }
  39. ```
  40.  
  41. ---
  42.  
  43. #### **3. Отсортировать строки по длине**
  44.  
  45. ```cpp
  46. #include <iostream>
  47. #include <vector>
  48. #include <algorithm>
  49.  
  50. void sortStringsByLength(std::vector<std::string>& strings) {
  51.     // напишите ваше решение
  52. }
  53.  
  54. int main() {
  55.     std::vector<std::string> strings = {"apple", "banana", "kiwi", "cherry"};
  56.     sortStringsByLength(strings);
  57.     for (const auto& str : strings) {
  58.         std::cout << str << " "; // Ожидаемый результат: kiwi apple cherry banana
  59.     }
  60.     return 0;
  61. }
  62. ```
  63.  
  64. ---
  65.  
  66. #### **4. Удалить все отрицательные числа из вектора**
  67.  
  68. ```cpp
  69. #include <iostream>
  70. #include <vector>
  71. #include <algorithm>
  72.  
  73. void removeNegativeNumbers(std::vector<int>& numbers) {
  74.     // напишите ваше решение
  75. }
  76.  
  77. int main() {
  78.     std::vector<int> numbers = {1, -2, 3, -4, 5};
  79.     removeNegativeNumbers(numbers);
  80.     for (int num : numbers) {
  81.         std::cout << num << " "; // Ожидаемый результат: 1 3 5
  82.     }
  83.     return 0;
  84. }
  85. ```
  86.  
  87. ---
  88.  
  89. #### **5. Проверить, есть ли хотя бы одно число больше 10**
  90.  
  91. ```cpp
  92. #include <iostream>
  93. #include <vector>
  94. #include <algorithm>
  95.  
  96. bool hasNumberGreaterThan10(const std::vector<int>& numbers) {
  97.     // напишите ваше решение
  98. }
  99.  
  100. int main() {
  101.     std::vector<int> numbers = {1, 5, 8, 12};
  102.     bool result = hasNumberGreaterThan10(numbers);
  103.     std::cout << (result ? "Есть число > 10" : "Нет числа > 10") << std::endl; // Ожидаемый результат: Есть число > 10
  104.     return 0;
  105. }
  106. ```
  107.  
  108. ---
  109.  
  110. #### **6. Найти первое число, которое делится на 3**
  111.  
  112. ```cpp
  113. #include <iostream>
  114. #include <vector>
  115. #include <algorithm>
  116.  
  117. int findFirstDivisibleBy3(const std::vector<int>& numbers) {
  118.     // напишите ваше решение
  119. }
  120.  
  121. int main() {
  122.     std::vector<int> numbers = {1, 2, 4, 9, 10};
  123.     int result = findFirstDivisibleBy3(numbers);
  124.     std::cout << "Первое число, делящееся на 3: " << result << std::endl; // Ожидаемый результат: 9
  125.     return 0;
  126. }
  127. ```
  128.  
  129. ---
  130.  
  131. #### **7. Преобразовать все строки в верхний регистр**
  132.  
  133. ```cpp
  134. #include <iostream>
  135. #include <vector>
  136. #include <algorithm>
  137. #include <cctype>
  138.  
  139. void toUpperCase(std::vector<std::string>& strings) {
  140.     // напишите ваше решение
  141. }
  142.  
  143. int main() {
  144.     std::vector<std::string> strings = {"hello", "world"};
  145.     toUpperCase(strings);
  146.     for (const auto& str : strings) {
  147.         std::cout << str << " "; // Ожидаемый результат: HELLO WORLD
  148.     }
  149.     return 0;
  150. }
  151. ```
  152.  
  153. ---
  154.  
  155. #### **8. Подсчитать количество строк, начинающихся с буквы 'A'**
  156.  
  157. ```cpp
  158. #include <iostream>
  159. #include <vector>
  160. #include <algorithm>
  161.  
  162. int countStringsStartingWithA(const std::vector<std::string>& strings) {
  163.     // напишите ваше решение
  164. }
  165.  
  166. int main() {
  167.     std::vector<std::string> strings = {"Apple", "Banana", "Apricot", "Cherry"};
  168.     int result = countStringsStartingWithA(strings);
  169.     std::cout << "Количество строк, начинающихся с 'A': " << result << std::endl; // Ожидаемый результат: 2
  170.     return 0;
  171. }
  172. ```
  173.  
  174. ---
  175.  
  176. #### **9. Найти произведение всех чисел в векторе**
  177.  
  178. ```cpp
  179. #include <iostream>
  180. #include <vector>
  181. #include <numeric>
  182.  
  183. int productOfNumbers(const std::vector<int>& numbers) {
  184.     // напишите ваше решение
  185. }
  186.  
  187. int main() {
  188.     std::vector<int> numbers = {1, 2, 3, 4};
  189.     int result = productOfNumbers(numbers);
  190.     std::cout << "Произведение чисел: " << result << std::endl; // Ожидаемый результат: 24
  191.     return 0;
  192. }
  193. ```
  194.  
  195. #### **10. Проверить, все ли числа в векторе положительные**
  196.  
  197. ```cpp
  198. #include <iostream>
  199. #include <vector>
  200. #include <algorithm>
  201.  
  202. bool areAllPositive(const std::vector<int>& numbers) {
  203.    // напишите ваше решение
  204. }
  205.  
  206. int main() {
  207.     std::vector<int> numbers = {1, 2, 3, 4};
  208.     bool result = areAllPositive(numbers);
  209.     std::cout << (result ? "Все числа положительные" : "Не все числа положительные") << std::endl; // Ожидаемый результат: Все числа положительные
  210.     return 0;
  211. }
  212. ```
  213.  
  214. #### **11. Найти среднее значение всех чисел в векторе**
  215.  
  216. ```cpp
  217. #include <iostream>
  218. #include <vector>
  219. #include <numeric>
  220.  
  221. double calculateAverage(const std::vector<int>& numbers) {
  222.      // напишите ваше решение
  223. }
  224.  
  225. int main() {
  226.     std::vector<int> numbers = {1, 2, 3, 4, 5};
  227.     double result = calculateAverage(numbers);
  228.     std::cout << "Среднее значение: " << result << std::endl; // Ожидаемый результат: 3.0
  229.     return 0;
  230. }
  231. ```
  232.  
  233. ---
  234.  
  235. #### **12. Найти количество уникальных элементов в векторе**
  236.  
  237. ```cpp
  238. #include <iostream>
  239. #include <vector>
  240. #include <algorithm>
  241.  
  242. int countUniqueElements(std::vector<int> numbers) {
  243.      // напишите ваше решение
  244. }
  245.  
  246. int main() {
  247.     std::vector<int> numbers = {1, 2, 2, 3, 4, 4, 5};
  248.     int result = countUniqueElements(numbers);
  249.     std::cout << "Количество уникальных элементов: " << result << std::endl; // Ожидаемый результат: 5
  250.     return 0;
  251. }
  252. ```
  253.  
  254. ---
  255.  
  256. #### **13. Удалить дубликаты из вектора**
  257.  
  258. ```cpp
  259. #include <iostream>
  260. #include <vector>
  261. #include <algorithm>
  262.  
  263. void removeDuplicates(std::vector<int>& numbers) {
  264.      // напишите ваше решение
  265. }
  266.  
  267. int main() {
  268.     std::vector<int> numbers = {1, 2, 2, 3, 4, 4, 5};
  269.     removeDuplicates(numbers);
  270.     for (int num : numbers) {
  271.         std::cout << num << " "; // Ожидаемый результат: 1 2 3 4 5
  272.     }
  273.     return 0;
  274. }
  275. ```
  276.  
  277. ---
  278.  
  279. #### **14. Проверить, является ли строка палиндромом**
  280.  
  281. ```cpp
  282. #include <iostream>
  283. #include <string>
  284. #include <algorithm>
  285.  
  286. bool isPalindrome(const std::string& str) {
  287.      // напишите ваше решение
  288. }
  289.  
  290. int main() {
  291.     std::string str = "Madam";
  292.     bool result = isPalindrome(str);
  293.     std::cout << (result ? "Строка является палиндромом" : "Строка не является палиндромом") << std::endl; // Ожидаемый результат: Строка является палиндромом
  294.     return 0;
  295. }
  296. ```
  297.  
  298. ---
  299.  
  300. #### **15. Найти все числа, которые делятся на 3 и 5**
  301.  
  302. ```cpp
  303. #include <iostream>
  304. #include <vector>
  305. #include <algorithm>
  306.  
  307. std::vector<int> findDivisibleBy3And5(const std::vector<int>& numbers) {
  308.      // напишите ваше решение
  309. }
  310.  
  311. int main() {
  312.     std::vector<int> numbers = {15, 10, 30, 9, 20};
  313.     std::vector<int> result = findDivisibleBy3And5(numbers);
  314.     for (int num : result) {
  315.         std::cout << num << " "; // Ожидаемый результат: 15 30
  316.     }
  317.     return 0;
  318. }
  319. ```
  320.  
  321. ---
  322.  
  323. #### **16. Отсортировать пары чисел по второму элементу**
  324.  
  325. ```cpp
  326. #include <iostream>
  327. #include <vector>
  328. #include <algorithm>
  329.  
  330. void sortBySecondElement(std::vector<std::pair<int, int>>& pairs) {
  331.      // напишите ваше решение
  332. }
  333.  
  334. int main() {
  335.     std::vector<std::pair<int, int>> pairs = {{1, 3}, {2, 1}, {3, 2}};
  336.     sortBySecondElement(pairs);
  337.     for (const auto& pair : pairs) {
  338.         std::cout << "(" << pair.first << ", " << pair.second << ") "; // Ожидаемый результат: (2, 1) (3, 2) (1, 3)
  339.     }
  340.     return 0;
  341. }
  342. ```
  343.  
  344. ---
  345.  
  346. #### **17. Найти минимальный элемент, который больше заданного значения**
  347.  
  348. ```cpp
  349. #include <iostream>
  350. #include <vector>
  351. #include <algorithm>
  352.  
  353. int findMinGreaterThan(const std::vector<int>& numbers, int value) {
  354.      // напишите ваше решение
  355. }
  356.  
  357. int main() {
  358.     std::vector<int> numbers = {1, 5, 8, 10, 3};
  359.     int result = findMinGreaterThan(numbers, 4);
  360.     std::cout << "Минимальный элемент больше 4: " << result << std::endl; // Ожидаемый результат: 5
  361.     return 0;
  362. }
  363. ```
  364.  
  365. ---
  366.  
  367. #### **18. Проверить, содержится ли подстрока в любой из строк вектора**
  368.  
  369. ```cpp
  370. #include <iostream>
  371. #include <vector>
  372. #include <algorithm>
  373.  
  374. bool containsSubstring(const std::vector<std::string>& strings, const std::string& substring) {
  375.      // напишите ваше решение
  376. }
  377.  
  378. int main() {
  379.     std::vector<std::string> strings = {"hello", "world", "hi"};
  380.     bool result = containsSubstring(strings, "lo");
  381.     std::cout << (result ? "Подстрока найдена" : "Подстрока не найдена") << std::endl; // Ожидаемый результат: Подстрока найдена
  382.     return 0;
  383. }
  384. ```
  385.  
  386. ---
  387.  
  388. #### **19. Преобразовать все числа в векторе в строки**
  389.  
  390. ```cpp
  391. #include <iostream>
  392. #include <vector>
  393. #include <algorithm>
  394. #include <string>
  395.  
  396. std::vector<std::string> convertNumbersToStrings(const std::vector<int>& numbers) {
  397.      // напишите ваше решение
  398. }
  399.  
  400. int main() {
  401.     std::vector<int> numbers = {1, 2, 3, 4};
  402.     std::vector<std::string> result = convertNumbersToStrings(numbers);
  403.     for (const auto& str : result) {
  404.         std::cout << str << " "; // Ожидаемый результат: "1" "2" "3" "4"
  405.     }
  406.     return 0;
  407. }
  408. ```
  409.  
  410. ---
  411.  
  412. #### **20. Найти индекс первого отрицательного числа**
  413.  
  414. ```cpp
  415. #include <iostream>
  416. #include <vector>
  417. #include <algorithm>
  418.  
  419. int findIndexOfFirstNegative(const std::vector<int>& numbers) {
  420.      // напишите ваше решение
  421. }
  422.  
  423. int main() {
  424.     std::vector<int> numbers = {1, 2, -3, 4, -5};
  425.     int result = findIndexOfFirstNegative(numbers);
  426.     std::cout << "Индекс первого отрицательного числа: " << result << std::endl; // Ожидаемый результат: 2
  427.     return 0;
  428. }
  429. ``
  430.  
Advertisement
Add Comment
Please, Sign In to add comment