Advertisement
BojidarDosev

1

Jan 18th, 2024
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.26 KB | None | 0 0
  1. #include <iostream>
  2. #include <set>
  3. #include <limits>
  4. using namespace std;
  5.  
  6. char minEvenAsciiIntersection(const set<char>& A, const set<char>& B) {
  7. int minEvenAscii = numeric_limits<int>::max();
  8. int minAscii = numeric_limits<int>::max();
  9. bool evenFound = false;
  10.  
  11. for (char elem : A) {
  12. if (B.find(elem) != B.end()) { // Проверка дали елементът се среща и в двете множества
  13. int asciiValue = static_cast<int>(elem);
  14. if (asciiValue % 2 == 0) { // Проверка за четност
  15. evenFound = true;
  16. if (asciiValue < minEvenAscii) {
  17. minEvenAscii = asciiValue;
  18. }
  19. }
  20. if (asciiValue < minAscii) {
  21. minAscii = asciiValue;
  22. }
  23. }
  24. }
  25.  
  26. if (evenFound) {
  27. return static_cast<char>(minEvenAscii);
  28. }
  29. else {
  30. return static_cast<char>(minAscii);
  31. }
  32. }
  33.  
  34. int main() {
  35. set<char> A = { 'e', 'r', 'D', 'U', 'w', 'd', 'B', 'x', 'N', 'z' };
  36. set<char> B = { 'm', 'd', 'r', 'D', 'l', 'w', 'А', 'W'};
  37.  
  38. char result = minEvenAsciiIntersection(A, B);
  39. cout << "The result is: " << result << std::endl;
  40.  
  41. return 0;
  42. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement