Advertisement
purmonth

RodCutting

Dec 8th, 2016
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.00 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #define Max 50000
  4.  
  5. int p[Max];
  6.  
  7.  
  8. int rodCutting(int p[],int num)
  9. {
  10.     int q, i, j ,t = 0;
  11.     for(j = 1;j < num+1;j ++)
  12.     {
  13.         q = -999;
  14.         for(i = 1;i < j+1;i ++)
  15.         {
  16.             //if(j % 2 == 0)
  17.             //    if(i == j-1)
  18.             //        break;
  19.             if(j > 5)
  20.                 if(p[i] == p[j-i])break;
  21.             t = p[i]+p[j-i];
  22.             if(t > q)q = t;
  23.             printf("\n%d %d %d %d\n",j,i,p[i],p[j-i]);
  24.         }
  25.         p[j] = q;
  26.     }
  27.     return p[num];
  28. }
  29.  
  30.  
  31.  
  32.  
  33.  
  34.  
  35. int main(){
  36.     int num,l = 1;
  37.     scanf("%d",&num);
  38.     int x, y;
  39.     while(1)
  40.     {
  41.         scanf("%d %d",&x,&y);
  42.         if(x==0 && y==0)break;
  43.         p[l] = y;
  44.         l++;
  45.     }
  46.     printf("%d\n",rodCutting(p, num));
  47.     return 0;
  48. }
  49.  
  50. /*
  51.  5
  52.  1 1
  53.  2 5
  54.  3 8
  55.  4 9
  56.  5 10
  57.  6 17
  58.  7 17
  59.  8 20
  60.  9 24
  61.  10 30
  62.  0 0
  63.  
  64.  16
  65.  1 1
  66.  2 5
  67.  3 8
  68.  4 9
  69.  5 10
  70.  6 17
  71.  7 17
  72.  8 20
  73.  9 24
  74.  10 30
  75.  11 40
  76.  12 26
  77.  0 0
  78. */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement