Run Code
|
API
|
Code Wall
|
Misc
|
Feedback
|
Login
|
Theme
|
Privacy
|
Patreon
TAREA M4- PERLAS TERMINADA
#include <stdio.h> #include <stdlib.h> #include <stdbool.h> typedef unsigned long UL; typedef unsigned long long ULL; // ----------------Prototipos----------------------- UL *input(UL *, ULL *); void output(UL *, ULL); int comparar(const void *, const void *); void ordenar(UL *, ULL ); bool verificar(UL *, ULL); // ---------------Función Principal----------------- int main(void) { UL *perlas; ULL total ; while (1) { perlas = NULL; total = 0; perlas = input(perlas, &total); if (total == 0) break; if ( total % 2 == 0 ) printf("NO\n"); else { qsort(perlas, total, sizeof(UL), comparar); if ( verificar(perlas, total) == false) printf("NO\n"); else { ordenar(perlas, total); //output(perlas, total); } } } return 0; } // ----------------------Funciones-------------------- UL *input(UL *ptrPerlas, ULL *N) { UL diametro; do { scanf("%lu", &diametro); if ( diametro == 0 ) break; ptrPerlas = (UL *) realloc(ptrPerlas, sizeof(UL) * ( (*N) + 1 ) ); if ( ptrPerlas == NULL) exit(EXIT_FAILURE); ptrPerlas[*N] = diametro; (*N) ++; }while(1); return ptrPerlas; } void output(UL *perlas, ULL N) { ULL i; for ( i = 0 ; i < N ; i ++ ) printf("%lu ", perlas[i]); printf("\n"); } int comparar(const void *pivot, const void *element) { UL *ptrPivot = (UL *) pivot; UL *ptrElement = (UL *) element; return *ptrPivot - *ptrElement; } bool verificar(UL *perlas, ULL N) { ULL i = 0; while ( i < N - 1) { if ( perlas[i] != perlas[i + 1]) return false; i += 2; } return true; } void ordenar(UL *perlas, ULL N) { UL array[N]; ULL copyN = N; ULL i = 0; ULL j = 0; UL dif = N - 1; array[(N - 1) / 2] = perlas[N - 1]; while ( i < N - 1) { array[j] = perlas[i]; array[j + dif] = perlas[i + 1]; dif -= 2; j ++; i += 2; } output(array, copyN); }
run
|
edit
|
history
|
help
0
2
Day 2 String Manipulation
Intro coding hello world
C_141120_factorial02
MergeSort
Loop-worksharing
ForU
Lab 7 blackjack v0.6
Exploiting uninitialized variable 2
sum of min and max