Advertisement
DIUBD

Rod_TEST_V3

Nov 2nd, 2017
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.14 KB | None | 0 0
  1. #include <string.h>
  2. #define MAXLEN 100
  3. int totalLen,partLen;
  4. const int priceOfLen[MAXLEN] = {0, 1, 5, 8, 9, 10, 17, 17, 20, 24, 30};
  5. int maxProfitOfLen[MAXLEN] = {}, partLenOfLen[MAXLEN];
  6. int maxProfit(int n)
  7. {
  8.     for (totalLen = 1; totalLen <= n; ++totalLen)
  9.     {
  10.         int tmp = 0;
  11.         for (partLen = 1; partLen <= totalLen; ++partLen)
  12.         {
  13.             int thisProfit = priceOfLen[partLen] + maxProfitOfLen[totalLen - partLen];
  14.             if (tmp < thisProfit)
  15.             {
  16.                 tmp = thisProfit;
  17.                 partLenOfLen[totalLen] = partLen; // record the left part (complete)
  18.             }
  19.         }
  20.         maxProfitOfLen[totalLen] = tmp;
  21.     }
  22.     return maxProfitOfLen[n];
  23. }
  24.  
  25. int main()
  26. {
  27.     int n;
  28.     puts("enter the length of the rod: ");
  29.     scanf("%d", &n);
  30.     printf("max profit is %d\nshow the cut method? (yes/no) ", maxProfit(n));
  31.     char answer[10];
  32.     scanf("%s", answer);
  33.     if (strcmp(answer, "yes") == 0)
  34.     {
  35.         while (n > 0)
  36.         {
  37.             printf("%d ", partLenOfLen[n]);
  38.             n -= partLenOfLen[n];
  39.         }
  40.     }
  41.     putchar('\n');
  42.     return 0;
  43. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement