Skip to content

Instantly share code, notes, and snippets.

@thinkphp
Created December 16, 2019 21:40
Show Gist options
  • Save thinkphp/a47f2196f199a3bde2593e0db17edd68 to your computer and use it in GitHub Desktop.
Save thinkphp/a47f2196f199a3bde2593e0db17edd68 to your computer and use it in GitHub Desktop.
add elements of array using the technique Divide Et Impera.
#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