Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <limits.h>
- int FindMAX (int*, int*&); // Прототип int ** -> int *&
- int FindMAX1(int*, int**);
- int FindMAX2(int*, int**);
- ///////////////////////////////////////////////////
- int main() //
- {
- int *pai = new int[23], // int *pai[23] -> int *pai;
- ai[23] = { 33, 22, 7, 99922222, 1, 334, 2, 777777, 55,
- 61, 17, 72, 8,11, 34, 4, 111000222, 12, 0 },
- i, cnt = FindMAX(ai, pai);
- for(i = 0; i < cnt; i++)
- {
- printf(" ai[%2d] = %9d " , i, ai[i]);
- printf("*pai[%2d] = %9d\n" , i, pai[i]);
- } getchar();
- delete[] pai;
- }
- // Решение Влада (2023 May 30)
- ///////////////////////////////////////////////////
- int FindMAX(int* x1, int*& x2) //
- {
- int nmaxIt = 0,
- size = 0,
- nmax = INT_MAX;
- for(; x1[size] != 0; ++size);
- for(size_t i = 0; i != size; ++ i) {
- for(size_t xi = 0; xi != size; ++xi)
- {
- if(nmaxIt < x1[xi] && x1[xi] < nmax) {
- nmaxIt = x1[xi];
- }
- }
- x2[size - 1 - i] = nmaxIt;
- nmax = nmaxIt;
- nmaxIt = 0;
- }
- return size;
- }
- // Моя версия (первоначальная)
- ////////////////////////////////////////////////////////////////////////////////////
- int FindMAX1(int* ai, int** pai) //
- {
- int Max = 0, // максимальный элемент
- *pM = 0, // указатель на максимальный элемент
- cnt = 0;
- while(ai[cnt])
- {
- if(Max < ai[cnt]) { Max = ai[cnt]; pM = &ai[cnt]; }
- cnt ++;
- } // Подсчитали кол-во элементов в массиве
- pai[cnt-1] = pM;
- int MM = 0, c = 0;
- for(int i = cnt-2; i >= 0; i--)
- {
- c = 0;
- while(ai[c])
- {
- if(ai[c] < Max) {
- if(MM < ai[c]) { MM = ai[c]; pM = &ai[c]; }
- } c++;
- }
- pai[i] = pM;
- Max = MM;
- MM = 0;
- }
- return cnt;
- }
- // Версия Александра (2021 September 12)
- ////////////////////////////////////////////////////////////////////
- int FindMAX2(int* ai, int** pai) //
- {
- int i = 0,
- cnt = 0;
- while(ai[i])
- {
- cnt++;
- i++;
- }
- for (int i = 0; i < cnt; i++) pai[i] = &ai[i];
- for (int i = 0; i < cnt; i++)
- {
- int *c;
- for (int j = cnt-2; j >= i; j--)
- {
- if ( *pai[j] > *pai[j+1] )
- {
- c = pai[j];
- pai[j] = pai[j+1];
- pai[j+1] = c;
- }
- }
- }
- return (cnt);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement