TwentyEight

N84 PR0GR4M

Jan 18th, 2018
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.71 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. #define MAX_PLAYERS 30
  5. #define BUDGET 300
  6.  
  7. typedef struct {
  8.     int price;
  9.     double pts;
  10. } player_t;
  11.  
  12. int get_players(player_t *p);
  13.  
  14. int main(int argc, char *argv[]) {
  15.     player_t pg[MAX_PLAYERS], sg[MAX_PLAYERS], sf[MAX_PLAYERS],
  16.     pf[MAX_PLAYERS], ce[MAX_PLAYERS];
  17.     int pgnum, sgnum, sfnum, pfnum, cenum, t[5], cost;
  18.     int a, b, c, d, e;
  19.     double currentsum, sum=0;
  20.    
  21.     printf("Enter point guards:\n");
  22.     pgnum = get_players(pg);
  23.     printf("Enter shooting guards:\n");
  24.     sgnum = get_players(sg);
  25.     printf("Enter small forwards:\n");
  26.     sfnum = get_players(sf);
  27.     printf("Enter power forwards:\n");
  28.     pfnum = get_players(pf);
  29.     printf("Enter centers:\n");
  30.     cenum = get_players(ce);
  31.  
  32.     for (a = pgnum - 1; a>=0; a--) {
  33.         for (b = sgnum - 1; b>=0; b--) {
  34.             for (c = sfnum - 1; c>=0; c--) {
  35.                 for (d = pfnum - 1; d>=0; d--) {
  36.                     for (e = cenum - 1; e>=0; e--) {
  37.                         cost = pg[a].price+sg[b].price+sf[c].price+pf[d].price+
  38.                         ce[e].price;
  39.                         if (cost > BUDGET) {
  40.                             break;
  41.                         }
  42.                         currentsum = pg[a].pts+sg[b].pts+sf[c].pts+pf[d].pts+
  43.                         ce[e].pts;
  44.                         if (currentsum >= sum) {
  45.                             if (currentsum == sum) {
  46.                                 printf("%.1f:", sum);
  47.                                 printf("%d %.1f  ", pg[a].price, pg[a].pts);
  48.                                 printf("%d %.1f  ", sg[b].price, sg[b].pts);
  49.                                 printf("%d %.1f  ", sf[c].price, sf[c].pts);
  50.                                 printf("%d %.1f  ", pf[d].price, pf[d].pts);
  51.                                 printf("%d %.1f\n", ce[e].price, ce[e].pts);
  52.                             }
  53.                             sum = currentsum;
  54.                             t[0] = a;
  55.                             t[1] = b;
  56.                             t[2] = c;
  57.                             t[3] = d;
  58.                             t[4] = e;
  59.                         }  
  60.                     }
  61.                     if (e == 0) {
  62.                         break;
  63.                     }
  64.                 }
  65.                 if (d == 0 && e == 0) {
  66.                     break;
  67.                 }
  68.             }
  69.             if (c == 0 && d == 0 && e == 0) {
  70.                 break;
  71.             }
  72.         }
  73.         if (b == 0 && c == 0 && d == 0 && e == 0) {
  74.             break;
  75.         }
  76.     }
  77.     printf("Point guard:    %3d\t%.1f\n", pg[t[0]].price, pg[t[0]].pts);
  78.     printf("Shooting guard: %3d\t%.1f\n", sg[t[1]].price, sg[t[1]].pts);
  79.     printf("Small forward:  %3d\t%.1f\n", sf[t[2]].price, sf[t[2]].pts);
  80.     printf("Power forward:  %3d\t%.1f\n", pf[t[3]].price, pf[t[3]].pts);
  81.     printf("Center:         %3d\t%.1f\n", ce[t[4]].price, ce[t[4]].pts);
  82.     cost = pg[t[0]].price + sg[t[1]].price + sf[t[2]].price + pf[t[3]].price +
  83.     ce[t[4]].price;
  84.     printf("Points: %.1f\tCost: %d\n", sum, cost);
  85.    
  86.     return 0;
  87. }
  88.  
  89. int get_players(player_t *p) {
  90.     int n=0, priceP;
  91.     double ptsP;
  92.    
  93.     while (scanf("%d %lf", &priceP, &ptsP) == 2) {
  94.         if (n>MAX_PLAYERS) {
  95.             printf("More than %d players entered.\nExit failure.\n",
  96.                 MAX_PLAYERS);
  97.             exit(EXIT_FAILURE);
  98.         }
  99.        
  100.         if (priceP==0||ptsP<1e-5) {
  101.             return n;
  102.         }
  103.        
  104.         p[n].price = priceP;
  105.         p[n].pts = ptsP;
  106.         n++;
  107.     }
  108.     return n;
  109. }
Add Comment
Please, Sign In to add comment