Skip to content

Instantly share code, notes, and snippets.

@toxicbloud
Last active November 4, 2021 19:51
Show Gist options
  • Save toxicbloud/be6b96a223bd68c7f76ed3a79e2f1388 to your computer and use it in GitHub Desktop.
Save toxicbloud/be6b96a223bd68c7f76ed3a79e2f1388 to your computer and use it in GitHub Desktop.
Fonctions récursives qui retournent le maximum d'un tableau
#include <iostream>
/**
* version triviale
*/
int findMaxRec(int A[], int n)
{
if (n == 1)
return A[0];
return std::max(A[n-1], findMaxRec(A, n-1));
}
/*
* version hardcore programmer c++
*/
int proMax(int *A,int n){
return (n-1)? std::max(A[n-1], proMax(A, n-1)): A[0];
}
/*
* version DIEU
* plus lent car on appel la fonction deux fois mais plus drôle
*/
int godMax(int *A,int n){
return (n-1)? (A[n-1] > godMax(A, n-1) ? A[n-1] : godMax(A, n-1)): A[0];
}
/**
* Ma version au DS d'Algo
*/
int maximum(int *A, int n,int i)
{
int temp;
if(i<n){
temp=maximum(A,n,i+1);
if(A[i]>temp)
temp= A[i];
}
return temp;
}
int main()
{
int A[] = {5, 4, 45, 6, -50, 10, 2};
int n = sizeof(A)/sizeof(A[0]);
int temp=proMax(A,n);
std::cout << " Maximum : " << temp <<std::endl;
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment