Untitled
unknown
plain_text
a year ago
1.6 kB
2
Indexable
Never
#include <stdlib.h> #include <stdio.h> #include <math.h> int cmp_int(const void* a, const void* b){ return *(int*)a - *(int*)b; } int cmp_char(const void* a, const void* b){ return *(char*)a - *(char *)b; } int cmp_double(const void* a, const void* b) { if(fabs(*(double*) a - *(double*) b) < 0.0001) return 0; if (*(double*) a - *(double*) b > 0) return 1; return -1; } int universalMax(void* arr, int arrsize, int arrtype, int (*cmp)(const void*, const void*)){ char* bytes = (char*)arr; char* max = (char*)arr; int indx = 0; for(int i=0; i < arrsize * arrtype; i+=arrtype){ if(cmp(bytes+i, max)>0){ indx = i/arrtype; max = bytes+i; } } return indx; } int main(){ int arrsize = 20; int arrtype; int res; scanf("%d", &arrtype); getchar(); int* arr = malloc(arrtype * arrsize); switch (arrtype) { case 1: for(int i=0; i < arrsize; i++){ scanf("%c", (char*)arr+i); getchar(); } res = universalMax(arr, arrsize, arrtype, cmp_char); break; case 4: for(int i=0; i < arrsize; i++){ scanf("%d", (int*)arr+i); } res = universalMax(arr, arrsize, arrtype, cmp_int); break; case 8: for(int i=0; i < arrsize; i++){ scanf("%lf", (double*)arr+i); } res = universalMax(arr, arrsize, arrtype, cmp_double); break; } printf("%d", res); return 0; }