Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * yahtzee_upper([2, 3, 5, 5, 6]) => 10
- yahtzee_upper([1, 1, 1, 1, 3]) => 4
- yahtzee_upper([1, 1, 1, 3, 3]) => 6
- yahtzee_upper([1, 2, 3, 4, 5]) => 5
- yahtzee_upper([6, 6, 6, 6, 6]) => 30
- */
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #include <stdlib.h>
- #include <errno.h>
- int cmp (const void * a, const void * b)
- {
- return ( *(int*)a - *(int*)b );
- }
- int main( int argc, char **argv )
- {
- FILE *fp;
- long holder = 0;
- long *rolls = malloc( (sizeof ( long ) * 1000 ) );
- int current_size = 1000;
- if (!rolls)
- {
- perror("malloc:");
- exit(-1);
- }
- fp = fopen("data", "r");
- int i = 0;
- while ( ( fscanf(fp, "%ld", &holder ) > 0 ) )
- {
- if (i > ( current_size - 1 ) )
- {
- current_size += 1000;
- rolls = realloc( rolls, ( sizeof( long ) * current_size ) );
- if (!rolls)
- {
- perror("realloc:");
- exit(-1);
- }
- }
- rolls[i] = holder;
- #ifdef DEBUG
- printf("Added %ld, to [%i] @ %p\n", holder, i, rolls[i]);
- #endif
- i++;
- }
- qsort( rolls, i, sizeof( long ), cmp );
- long val = 0;
- int repeat = 1;
- long max = 0;
- for ( int j = 0; j < i; j++ )
- {
- if (rolls[j] == rolls[j+1])
- {
- val = rolls[j];
- repeat++;
- }
- else
- {
- int newmax = val * repeat;
- if (newmax > max)
- {
- max = newmax;
- printf("val = %ld, rep = %d\n", val, repeat);
- printf("max = %ld\n", max);
- }
- val = 0;
- repeat = 1;
- }
- }
- printf("High Score = %ld\n", max);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement