Advertisement
Spocoman

08. Equal Pairs

Sep 8th, 2023
880
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.44 KB | None | 0 0
  1. #include <iostream>
  2.  
  3. using namespace std;
  4.  
  5. int main() {
  6.     int pairCount, firstNum, secondNum;
  7.     cin >> pairCount >> firstNum >> secondNum;
  8.  
  9.     int equalValue = firstNum + secondNum;
  10.     int maxdiff = 0;
  11.  
  12.     for (int i = 1; i < pairCount; i++) {
  13.         cin >> firstNum >> secondNum;
  14.  
  15.         int currentValue = firstNum + secondNum;
  16.         int currentDiff = abs(equalValue - currentValue);
  17.  
  18.         if (currentDiff > maxdiff) {
  19.             maxdiff = currentDiff;
  20.         }
  21.         else {
  22.             equalValue = currentValue;
  23.         }
  24.     }
  25.  
  26.     if (maxdiff == 0) {
  27.         cout << "Yes, value=" << equalValue << endl;
  28.     }
  29.     else {
  30.         cout << "No, maxdiff=" << maxdiff << endl;
  31.     }
  32.  
  33.     return 0;
  34. }
  35.  
  36. Решение с тернарен оператор:
  37.  
  38. #include <iostream>
  39. #include <string>
  40.  
  41. using namespace std;
  42.  
  43. int main() {
  44.     int pairCount, firstNum, secondNum;
  45.     cin >> pairCount >> firstNum >> secondNum;
  46.  
  47.     int equalValue = firstNum + secondNum;
  48.     int maxdiff = 0;
  49.  
  50.     for (int i = 1; i < pairCount; i++) {
  51.         cin >> firstNum >> secondNum;
  52.  
  53.         int currentValue = firstNum + secondNum;
  54.         int currentDiff = abs(equalValue - currentValue);
  55.  
  56.         currentDiff > maxdiff ? maxdiff = currentDiff : equalValue = currentValue;
  57.     }
  58.  
  59.     cout << (maxdiff == 0 ? "Yes, value=" + to_string(equalValue) : "No, maxdiff=" + to_string(maxdiff)) << endl;
  60.  
  61.     return 0;
  62. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement