#### **1. Найти сумму всех четных чисел в векторе**
```cpp
#include <iostream>
#include <vector>
#include <numeric>
int sumEvenNumbers(const std::vector<int>& numbers) {
// напишите ваше решение
}
int main() {
std::vector<int> numbers = {1, 2, 3, 4, 5, 6};
int result = sumEvenNumbers(numbers);
std::cout << "Сумма четных чисел: " << result << std::endl; // Ожидаемый результат: 12
return 0;
}
```
---
#### **2. Найти максимальный элемент в векторе**
```cpp
#include <iostream>
#include <vector>
#include <algorithm>
int findMaxElement(const std::vector<int>& numbers) {
// напишите ваше решение
}
int main() {
std::vector<int> numbers = {10, 20, 5, 7, 30};
int result = findMaxElement(numbers);
std::cout << "Максимальный элемент: " << result << std::endl; // Ожидаемый результат: 30
return 0;
}
```
---
#### **3. Отсортировать строки по длине**
```cpp
#include <iostream>
#include <vector>
#include <algorithm>
void sortStringsByLength(std::vector<std::string>& strings) {
// напишите ваше решение
}
int main() {
std::vector<std::string> strings = {"apple", "banana", "kiwi", "cherry"};
sortStringsByLength(strings);
for (const auto& str : strings) {
std::cout << str << " "; // Ожидаемый результат: kiwi apple cherry banana
}
return 0;
}
```
---
#### **4. Удалить все отрицательные числа из вектора**
```cpp
#include <iostream>
#include <vector>
#include <algorithm>
void removeNegativeNumbers(std::vector<int>& numbers) {
// напишите ваше решение
}
int main() {
std::vector<int> numbers = {1, -2, 3, -4, 5};
removeNegativeNumbers(numbers);
for (int num : numbers) {
std::cout << num << " "; // Ожидаемый результат: 1 3 5
}
return 0;
}
```
---
#### **5. Проверить, есть ли хотя бы одно число больше 10**
```cpp
#include <iostream>
#include <vector>
#include <algorithm>
bool hasNumberGreaterThan10(const std::vector<int>& numbers) {
// напишите ваше решение
}
int main() {
std::vector<int> numbers = {1, 5, 8, 12};
bool result = hasNumberGreaterThan10(numbers);
std::cout << (result ? "Есть число > 10" : "Нет числа > 10") << std::endl; // Ожидаемый результат: Есть число > 10
return 0;
}
```
---
#### **6. Найти первое число, которое делится на 3**
```cpp
#include <iostream>
#include <vector>
#include <algorithm>
int findFirstDivisibleBy3(const std::vector<int>& numbers) {
// напишите ваше решение
}
int main() {
std::vector<int> numbers = {1, 2, 4, 9, 10};
int result = findFirstDivisibleBy3(numbers);
std::cout << "Первое число, делящееся на 3: " << result << std::endl; // Ожидаемый результат: 9
return 0;
}
```
---
#### **7. Преобразовать все строки в верхний регистр**
```cpp
#include <iostream>
#include <vector>
#include <algorithm>
#include <cctype>
void toUpperCase(std::vector<std::string>& strings) {
// напишите ваше решение
}
int main() {
std::vector<std::string> strings = {"hello", "world"};
toUpperCase(strings);
for (const auto& str : strings) {
std::cout << str << " "; // Ожидаемый результат: HELLO WORLD
}
return 0;
}
```
---
#### **8. Подсчитать количество строк, начинающихся с буквы 'A'**
```cpp
#include <iostream>
#include <vector>
#include <algorithm>
int countStringsStartingWithA(const std::vector<std::string>& strings) {
// напишите ваше решение
}
int main() {
std::vector<std::string> strings = {"Apple", "Banana", "Apricot", "Cherry"};
int result = countStringsStartingWithA(strings);
std::cout << "Количество строк, начинающихся с 'A': " << result << std::endl; // Ожидаемый результат: 2
return 0;
}
```
---
#### **9. Найти произведение всех чисел в векторе**
```cpp
#include <iostream>
#include <vector>
#include <numeric>
int productOfNumbers(const std::vector<int>& numbers) {
// напишите ваше решение
}
int main() {
std::vector<int> numbers = {1, 2, 3, 4};
int result = productOfNumbers(numbers);
std::cout << "Произведение чисел: " << result << std::endl; // Ожидаемый результат: 24
return 0;
}
```
#### **10. Проверить, все ли числа в векторе положительные**
```cpp
#include <iostream>
#include <vector>
#include <algorithm>
bool areAllPositive(const std::vector<int>& numbers) {
// напишите ваше решение
}
int main() {
std::vector<int> numbers = {1, 2, 3, 4};
bool result = areAllPositive(numbers);
std::cout << (result ? "Все числа положительные" : "Не все числа положительные") << std::endl; // Ожидаемый результат: Все числа положительные
return 0;
}
```
#### **11. Найти среднее значение всех чисел в векторе**
```cpp
#include <iostream>
#include <vector>
#include <numeric>
double calculateAverage(const std::vector<int>& numbers) {
// напишите ваше решение
}
int main() {
std::vector<int> numbers = {1, 2, 3, 4, 5};
double result = calculateAverage(numbers);
std::cout << "Среднее значение: " << result << std::endl; // Ожидаемый результат: 3.0
return 0;
}
```
---
#### **12. Найти количество уникальных элементов в векторе**
```cpp
#include <iostream>
#include <vector>
#include <algorithm>
int countUniqueElements(std::vector<int> numbers) {
// напишите ваше решение
}
int main() {
std::vector<int> numbers = {1, 2, 2, 3, 4, 4, 5};
int result = countUniqueElements(numbers);
std::cout << "Количество уникальных элементов: " << result << std::endl; // Ожидаемый результат: 5
return 0;
}
```
---
#### **13. Удалить дубликаты из вектора**
```cpp
#include <iostream>
#include <vector>
#include <algorithm>
void removeDuplicates(std::vector<int>& numbers) {
// напишите ваше решение
}
int main() {
std::vector<int> numbers = {1, 2, 2, 3, 4, 4, 5};
removeDuplicates(numbers);
for (int num : numbers) {
std::cout << num << " "; // Ожидаемый результат: 1 2 3 4 5
}
return 0;
}
```
---
#### **14. Проверить, является ли строка палиндромом**
```cpp
#include <iostream>
#include <string>
#include <algorithm>
bool isPalindrome(const std::string& str) {
// напишите ваше решение
}
int main() {
std::string str = "Madam";
bool result = isPalindrome(str);
std::cout << (result ? "Строка является палиндромом" : "Строка не является палиндромом") << std::endl; // Ожидаемый результат: Строка является палиндромом
return 0;
}
```
---
#### **15. Найти все числа, которые делятся на 3 и 5**
```cpp
#include <iostream>
#include <vector>
#include <algorithm>
std::vector<int> findDivisibleBy3And5(const std::vector<int>& numbers) {
// напишите ваше решение
}
int main() {
std::vector<int> numbers = {15, 10, 30, 9, 20};
std::vector<int> result = findDivisibleBy3And5(numbers);
for (int num : result) {
std::cout << num << " "; // Ожидаемый результат: 15 30
}
return 0;
}
```
---
#### **16. Отсортировать пары чисел по второму элементу**
```cpp
#include <iostream>
#include <vector>
#include <algorithm>
void sortBySecondElement(std::vector<std::pair<int, int>>& pairs) {
// напишите ваше решение
}
int main() {
std::vector<std::pair<int, int>> pairs = {{1, 3}, {2, 1}, {3, 2}};
sortBySecondElement(pairs);
for (const auto& pair : pairs) {
std::cout << "(" << pair.first << ", " << pair.second << ") "; // Ожидаемый результат: (2, 1) (3, 2) (1, 3)
}
return 0;
}
```
---
#### **17. Найти минимальный элемент, который больше заданного значения**
```cpp
#include <iostream>
#include <vector>
#include <algorithm>
int findMinGreaterThan(const std::vector<int>& numbers, int value) {
// напишите ваше решение
}
int main() {
std::vector<int> numbers = {1, 5, 8, 10, 3};
int result = findMinGreaterThan(numbers, 4);
std::cout << "Минимальный элемент больше 4: " << result << std::endl; // Ожидаемый результат: 5
return 0;
}
```
---
#### **18. Проверить, содержится ли подстрока в любой из строк вектора**
```cpp
#include <iostream>
#include <vector>
#include <algorithm>
bool containsSubstring(const std::vector<std::string>& strings, const std::string& substring) {
// напишите ваше решение
}
int main() {
std::vector<std::string> strings = {"hello", "world", "hi"};
bool result = containsSubstring(strings, "lo");
std::cout << (result ? "Подстрока найдена" : "Подстрока не найдена") << std::endl; // Ожидаемый результат: Подстрока найдена
return 0;
}
```
---
#### **19. Преобразовать все числа в векторе в строки**
```cpp
#include <iostream>
#include <vector>
#include <algorithm>
#include <string>
std::vector<std::string> convertNumbersToStrings(const std::vector<int>& numbers) {
// напишите ваше решение
}
int main() {
std::vector<int> numbers = {1, 2, 3, 4};
std::vector<std::string> result = convertNumbersToStrings(numbers);
for (const auto& str : result) {
std::cout << str << " "; // Ожидаемый результат: "1" "2" "3" "4"
}
return 0;
}
```
---
#### **20. Найти индекс первого отрицательного числа**
```cpp
#include <iostream>
#include <vector>
#include <algorithm>
int findIndexOfFirstNegative(const std::vector<int>& numbers) {
// напишите ваше решение
}
int main() {
std::vector<int> numbers = {1, 2, -3, 4, -5};
int result = findIndexOfFirstNegative(numbers);
std::cout << "Индекс первого отрицательного числа: " << result << std::endl; // Ожидаемый результат: 2
return 0;
}
``