Advertisement
Guest User

Untitled

a guest
Oct 23rd, 2016
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.35 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. typedef struct
  5. {
  6. int mass;
  7. int strengh;
  8. } athlet ;
  9.  
  10. void bubble (int n, athlet* men);
  11. int build ( const athlet men[], int n );
  12.  
  13. int main ()
  14. {
  15. int n;
  16. printf ("Program 4.1 ATHLETS\n");
  17. printf ("Please, enter number of athlets\n");
  18. scanf ("%d", &n);
  19. athlet* men = (athlet*)calloc(n, sizeof(athlet)); // Массив задан верно
  20. int i = 0;
  21. printf("Please, enter athlets' stats\n");
  22. for (i = 0; i < n; i++)
  23. {
  24. printf ("mass[%d] = ", i + 1);
  25. scanf ("%d", &men[i].mass);
  26. printf ("\n");
  27. printf ("strengh[%d] = ", i+1);
  28. scanf ("%d", &men[i].strengh);
  29. printf ("\n");
  30. }
  31.  
  32. for (i = 0; i < n; i++)
  33. {
  34. printf ("%2d: m : %3d, s : %3d\n", i + 1, men[i].mass, men[i].strengh);
  35. }
  36.  
  37. printf("sorted : \n");
  38. bubble(n, men);
  39. for ( i = 0; i < n; i++)
  40. printf ("%2d : m.%3d, s.%3d\n", i+1, men[i].mass, men[i].strengh);
  41.  
  42. printf ("Tower max height is : %d\n", build ( men, n ) );
  43. }
  44.  
  45. // Передаём указатель где он начинается, и работаем с массивом
  46. void bubble (int n, athlet* men)
  47. {
  48. athlet buf;
  49. int j, i;
  50. for ( i = 0; i < n - 1; i++)
  51. for ( j = 0; j < n - 1; j++)
  52. if ( men[j].mass > men[j + 1].mass)
  53. {
  54. buf = men[j];
  55. men[j] = men[j + 1];
  56. men[j + 1] = buf;
  57. }
  58. }
  59.  
  60. /* Создаем башню с самого легкого - тот самый "жадный" алгоритм */
  61. int build ( const athlet men[], int n )
  62. {
  63. int i, count = 1, fullmass = men[0].mass;
  64. for ( i = 1; i < ( n - 1 ); i++)
  65. {
  66. if ( men[i].strengh > fullmass)
  67. {
  68. fullmass += men[i].mass;
  69. count++;
  70. }
  71. }
  72. return count;
  73. }
  74. printf("sorted : \n");
  75. bubble(n, men);
  76. for ( i = 0; i < n; i++)
  77. printf ("%2d : m.%3d, s.%3d\n", i+1, men[i].mass, men[i].strengh);
  78. printf ("Tower max height is : %d\n", build ( men, n ) );
  79. }
  80.  
  81. // Передаём указатель где он начинается, и работаем с массивом
  82. void bubble (int n, athlet* men)
  83. {
  84. athlet buf;
  85. int j, i;
  86. for ( i = 0; i < n - 1; i++)
  87. for ( j = 0; j < n - 1; j++)
  88. if ( men[j].mass > men[j + 1].mass)
  89. {
  90. buf = men[j];
  91. men[j] = men[j + 1];
  92. men[j + 1] = buf;
  93. }
  94. }
  95.  
  96. int build ( const athlet men[], int n )
  97. {
  98. int i, count = 1, fullmass = men[0].mass;
  99. for ( i = 1; i < ( n - 1 ); i++)
  100. {
  101. if ( men[i].strengh > fullmass)
  102. {
  103. fullmass += men[i].mass;
  104. count++;
  105. }
  106. }
  107. return count;
  108. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement