Advertisement
Guest User

Untitled

a guest
Apr 1st, 2020
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.24 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. //Structura pentru jucator:
  5. typedef struct Player
  6. {
  7. char *last_name;
  8. char *first_name;
  9. int score;
  10. } Player;
  11.  
  12. //Structura pentru tara:
  13. typedef struct Country
  14. {
  15. char *name;
  16. int nr_players;
  17. int global_score;
  18. Player *players;
  19. } Country;
  20.  
  21. struct Elem
  22. {
  23. Country Countries;
  24. struct Elem *next, *prev;
  25. };
  26. typedef struct Elem Node;
  27. void addAtBeginning(Node **head, Country aux)
  28. {
  29. Node *newNode = (Node *)malloc(sizeof(Node));
  30. newNode->Countries = aux;
  31. newNode->next = newNode;
  32. newNode->prev = newNode;
  33. *head = newNode;
  34. }
  35. void iend(Node **head, Country aux)
  36. {
  37. if (*head == NULL)
  38. {
  39. addAtBeginning(&*head, aux);
  40. return;
  41. }
  42. Node *headcopy = *head;
  43. Node *newNode = (Node *)malloc(sizeof(Node));
  44. while (headcopy->next != *head)
  45. headcopy = headcopy->next;
  46. headcopy->next=newNode;
  47. newNode->prev=headcopy;
  48. newNode->Countries=aux;
  49. newNode->next=*head;
  50. (*head)->prev=newNode;
  51.  
  52. }
  53. Node *write_list(int n_countries)
  54. {
  55. char s[20];
  56. Node *head=NULL;
  57. for (int i = 0; i < n_countries; i++)
  58. {
  59. Country aux;
  60. scanf("%d", &aux.nr_players);
  61. aux.players = malloc(sizeof(Player) * aux.nr_players);
  62. scanf("%s", s);
  63. aux.name = malloc((strlen(s) + 1) * sizeof(char));
  64. strcpy(aux.name, s);
  65. for (int j = 0; j < aux.nr_players; j++)
  66. {
  67. scanf("%s", s);
  68. aux.players[j].last_name = malloc((strlen(s) + 1) * sizeof(char));
  69. strcpy(aux.players[j].last_name, s);
  70. scanf("%s", s);
  71. aux.players[j].first_name = malloc((strlen(s) + 1) * sizeof(char));
  72. strcpy(aux.players[j].first_name, s);
  73. scanf("%d", &aux.players[j].score);
  74. }
  75. iend(&head, aux);
  76. }
  77. //fac o structura tara noua
  78. Country sentinel;
  79. //pun nr de playeri =0 ca sa am un element cu care sa pot identifica santinela
  80. sentinel.nr_players=0;
  81. //o adaug in lista
  82. iend(&head,sentinel);
  83. return head;
  84. }
  85. /*void print(Node *head)
  86. {
  87. Node *headcopy = head;
  88.  
  89. while (headcopy->next != head)
  90. {
  91. printf("%s\n", headcopy->Countries.name);
  92. for (int i = 0; i < headcopy->Countries.nr_players; i++)
  93. printf("%s %s %d\n", headcopy->Countries.players[i].last_name, headcopy->Countries.players[i].first_name, headcopy->Countries.players[i].score);
  94. headcopy = headcopy->next;
  95. }
  96. printf("%s\n", headcopy->Countries.name);
  97. for (int i = 0; i < headcopy->Countries.nr_players; i++)
  98. printf("%s %s %d\n", headcopy->Countries.players[i].last_name, headcopy->Countries.players[i].first_name, headcopy->Countries.players[i].score);
  99. }*/
  100. int eliminare_tari(int n_countries)
  101. {
  102. int nr=1;
  103. while(nr<=n_countries)
  104. nr=nr*2;
  105. nr=nr/2;
  106. return nr;
  107. // printf("Se vor elimina %d tari.\n",n_countries-nr);
  108. //*n_countries_list=nr;
  109. }
  110. void print(Node *head)
  111. {
  112. Node *headcopy = head;
  113.  
  114. while (headcopy->Countries.nr_players != 0)
  115. {
  116. printf("%s\n", headcopy->Countries.name);
  117. for (int i = 0; i < headcopy->Countries.nr_players; i++)
  118. printf("%s %s %d\n", headcopy->Countries.players[i].last_name, headcopy->Countries.players[i].first_name, headcopy->Countries.players[i].score);
  119. headcopy = headcopy->next;
  120. }
  121. }
  122. float Scor_initial(Node **head)
  123. {
  124. Node *headcopy=*head;
  125. // while(headcopy->Countries.nr_players!=0)
  126. {
  127. int s=0;
  128. float initial_score;
  129. for(int i=0;i<headcopy->Countries.nr_players;i++)
  130. s=s+headcopy->Countries.players[i].score;
  131. initial_score=s/headcopy->Countries.nr_players;
  132. return initial_score;
  133. }
  134. }
  135. void delete(Node**head, Country aux) /*val este valoarea stocata in nodul de sters*/
  136. {
  137. if (*head==NULL) return;
  138. if (*head==(*head)->next && (*head)->val==val) /*acesta e singurul nod din lista*/
  139. {
  140. free(*head);
  141. *head=NULL; /*lista e acum goala*/
  142. return;
  143. }
  144. Node *headcopy=(*head)->next;
  145. Node *aux=*head;
  146. while (headcopy!=*head) /*daca acesta nu e β€œprimul” element*/
  147. {
  148. if (headcopy->val!=val)
  149. {
  150. aux=headcopy;
  151. headcopy=headcopy->next;
  152. }
  153. else
  154. {
  155. aux->next=headcopy->next;
  156. free(headcopy);
  157. return;
  158. }
  159. }
  160. void delete_minim(Node **head,int *n_countries)
  161. {
  162. float min_scor_initial=Scor_initial(&*head);
  163. Node *headcopy=*head;
  164. while((*n_countries)>eliminare_tari(n_countries))
  165. {
  166. while(headcopy->Countries.nr_players!=0)
  167. {
  168. if (min_scor_initial>Scor_initial(&*head)) min_scor_initial=Scor_initial(&*head);
  169. headcopy=headcopy->next;
  170. }
  171. while(headcopy->Countries.nr_players!=0)
  172. if(Scor_initial(&*head)==min_scor_initial) //sterge head;
  173. (*n_countries)--;
  174. }
  175.  
  176. }
  177. int main()
  178. {
  179. Node *head;
  180. int n_countries;
  181. scanf("%d", &n_countries);
  182. head = write_list(n_countries);
  183. printf("Nr tari:%d\n", n_countries);
  184. print(head);
  185. printf("\n");
  186. return 0;
  187. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement