Created
December 16, 2019 21:40
-
-
Save thinkphp/a47f2196f199a3bde2593e0db17edd68 to your computer and use it in GitHub Desktop.
add elements of array using the technique Divide Et Impera.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <malloc.h> | |
#define FIN "coliban.in" | |
int euclid(int a, int b) { | |
if(b == 0) return a; | |
else return euclid(b, a % b); | |
} | |
int divide_et_impera(int *p, int lo, int hi) { | |
int m, a, b; | |
if(lo == hi) return *(p + lo); | |
else { | |
m = (lo + hi)>>1; | |
a = divide_et_impera(p, lo, m); | |
b = divide_et_impera(p, m + 1, hi); | |
return euclid(a, b); | |
} | |
} | |
int main() { | |
int i, n, *ptr, out; | |
freopen(FIN,"r", stdin); | |
scanf("%d", &n); | |
ptr = (int*)malloc(sizeof(int) * n); | |
for(i = 0; i < n; ++i) scanf("%d", ptr + i); | |
out = divide_et_impera(ptr, 0, n - 1); | |
for(i = 0; i < n; ++i) printf("%d ", *(ptr + i)); | |
printf("\n"); | |
printf("Min = %d\n", out); | |
return(0); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment