Advertisement
Caneq

lb5.4.8

Feb 21st, 2019
408
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.10 KB | None | 0 0
  1. #include "pch.h"
  2. #include <iostream>
  3. using namespace std;
  4.  
  5. int& max(int* arr, int N, int nepodh = LONG_MAX) {
  6.     int indmax = 0;
  7.     bool repet = false;
  8.     int itercount = 0;
  9.     for (int i = 1; i < N; i++) {
  10.         if (arr[i] > arr[indmax] && arr[i] < nepodh) {
  11.             itercount++;
  12.             indmax = i;
  13.             repet = false;
  14.             continue;
  15.         }
  16.         else if (arr[i] == arr[indmax]) {
  17.             repet = true;
  18.         }
  19.     }
  20.     if (!repet) return arr[indmax];
  21.     else {
  22.         if (!itercount) {
  23.             indmax = 0;
  24.             for (int i = 1; i < N; i++)
  25.                 if (arr[i] > arr[indmax])
  26.                     indmax = i;
  27.             return arr[indmax];
  28.         }
  29.         nepodh = arr[indmax];
  30.         return max(arr, N, nepodh);
  31.     }
  32. }
  33. void print(int* arr, int N) {
  34.     for (int i = 0; i < N; i++) {
  35.         cout << arr[i] << " ";
  36.     }
  37.     cout << endl;
  38. }
  39.  
  40.  
  41. int main(){
  42.     int A[] = { 11,11,2,2,3,3,4, 4, 10,10 };
  43.     int N = sizeof(A) / 4;
  44.     int n;
  45.     cout << "n = ";
  46.     cin >> n;
  47.     if (n) {
  48.         int* a = new int[n];
  49.         for (int i = 0; i < n; i++) {
  50.             cin >> a[i];
  51.         }
  52.         cout << max(a, n) << endl;
  53.         max(a, n) = 0;
  54.         print(a, n);
  55.         return 0;
  56.     }
  57.  
  58.     cout << "max = " << max(A, N) << endl;
  59.     max(A, N) = 0;
  60.     print(A, N);
  61. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement