Advertisement
Guest User

Untitled

a guest
Nov 13th, 2019
242
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.47 KB | None | 0 0
  1. /*
  2. * yahtzee_upper([2, 3, 5, 5, 6]) => 10
  3. yahtzee_upper([1, 1, 1, 1, 3]) => 4
  4. yahtzee_upper([1, 1, 1, 3, 3]) => 6
  5. yahtzee_upper([1, 2, 3, 4, 5]) => 5
  6. yahtzee_upper([6, 6, 6, 6, 6]) => 30
  7. */
  8.  
  9. #include <stdio.h>
  10. #include <stdlib.h>
  11. #include <string.h>
  12. #include <stdlib.h>
  13. #include <errno.h>
  14.  
  15. int cmp (const void * a, const void * b)
  16. {
  17. return ( *(int*)a - *(int*)b );
  18. }
  19.  
  20.  
  21. int main( int argc, char **argv )
  22. {
  23. FILE *fp;
  24. long holder = 0;
  25. long *rolls = malloc( (sizeof ( long ) * 1000 ) );
  26.  
  27. int current_size = 1000;
  28.  
  29. if (!rolls)
  30. {
  31. perror("malloc:");
  32. exit(-1);
  33. }
  34.  
  35.  
  36.  
  37. fp = fopen("data", "r");
  38.  
  39. int i = 0;
  40.  
  41. while ( ( fscanf(fp, "%ld", &holder ) > 0 ) )
  42. {
  43. if (i > ( current_size - 1 ) )
  44. {
  45. current_size += 1000;
  46. rolls = realloc( rolls, ( sizeof( long ) * current_size ) );
  47. if (!rolls)
  48. {
  49. perror("realloc:");
  50. exit(-1);
  51. }
  52. }
  53. rolls[i] = holder;
  54. #ifdef DEBUG
  55. printf("Added %ld, to [%i] @ %p\n", holder, i, rolls[i]);
  56. #endif
  57. i++;
  58. }
  59.  
  60. qsort( rolls, i, sizeof( long ), cmp );
  61.  
  62. long val = 0;
  63. int repeat = 1;
  64. long max = 0;
  65.  
  66.  
  67. for ( int j = 0; j < i; j++ )
  68. {
  69. if (rolls[j] == rolls[j+1])
  70. {
  71. val = rolls[j];
  72. repeat++;
  73. }
  74. else
  75. {
  76. int newmax = val * repeat;
  77. if (newmax > max)
  78. {
  79. max = newmax;
  80. printf("val = %ld, rep = %d\n", val, repeat);
  81. printf("max = %ld\n", max);
  82. }
  83. val = 0;
  84. repeat = 1;
  85. }
  86. }
  87.  
  88. printf("High Score = %ld\n", max);
  89. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement