Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- typedef struct
- {
- int mass;
- int strengh;
- } athlet ;
- void bubble (int n, athlet* men);
- int build ( const athlet men[], int n );
- int main ()
- {
- int n;
- printf ("Program 4.1 ATHLETS\n");
- printf ("Please, enter number of athlets\n");
- scanf ("%d", &n);
- athlet* men = (athlet*)calloc(n, sizeof(athlet)); // Массив задан верно
- int i = 0;
- printf("Please, enter athlets' stats\n");
- for (i = 0; i < n; i++)
- {
- printf ("mass[%d] = ", i + 1);
- scanf ("%d", &men[i].mass);
- printf ("\n");
- printf ("strengh[%d] = ", i+1);
- scanf ("%d", &men[i].strengh);
- printf ("\n");
- }
- for (i = 0; i < n; i++)
- {
- printf ("%2d: m : %3d, s : %3d\n", i + 1, men[i].mass, men[i].strengh);
- }
- printf("sorted : \n");
- bubble(n, men);
- for ( i = 0; i < n; i++)
- printf ("%2d : m.%3d, s.%3d\n", i+1, men[i].mass, men[i].strengh);
- printf ("Tower max height is : %d\n", build ( men, n ) );
- }
- // Передаём указатель где он начинается, и работаем с массивом
- void bubble (int n, athlet* men)
- {
- athlet buf;
- int j, i;
- for ( i = 0; i < n - 1; i++)
- for ( j = 0; j < n - 1; j++)
- if ( men[j].mass > men[j + 1].mass)
- {
- buf = men[j];
- men[j] = men[j + 1];
- men[j + 1] = buf;
- }
- }
- /* Создаем башню с самого легкого - тот самый "жадный" алгоритм */
- int build ( const athlet men[], int n )
- {
- int i, count = 1, fullmass = men[0].mass;
- for ( i = 1; i < ( n - 1 ); i++)
- {
- if ( men[i].strengh > fullmass)
- {
- fullmass += men[i].mass;
- count++;
- }
- }
- return count;
- }
- printf("sorted : \n");
- bubble(n, men);
- for ( i = 0; i < n; i++)
- printf ("%2d : m.%3d, s.%3d\n", i+1, men[i].mass, men[i].strengh);
- printf ("Tower max height is : %d\n", build ( men, n ) );
- }
- // Передаём указатель где он начинается, и работаем с массивом
- void bubble (int n, athlet* men)
- {
- athlet buf;
- int j, i;
- for ( i = 0; i < n - 1; i++)
- for ( j = 0; j < n - 1; j++)
- if ( men[j].mass > men[j + 1].mass)
- {
- buf = men[j];
- men[j] = men[j + 1];
- men[j + 1] = buf;
- }
- }
- int build ( const athlet men[], int n )
- {
- int i, count = 1, fullmass = men[0].mass;
- for ( i = 1; i < ( n - 1 ); i++)
- {
- if ( men[i].strengh > fullmass)
- {
- fullmass += men[i].mass;
- count++;
- }
- }
- return count;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement