fork download
  1. #include<cstdio>
  2. #include<cstring>
  3. #include<cmath>
  4.  
  5. void Input_Data(int item[], int jum);
  6. void Show_Data(int item[], int jum);
  7. void Sort_Density(int *no, float *weight, float *profit, int *pieces, double *density, int c);
  8.  
  9. typedef struct
  10. {
  11. int b, no;
  12. float w, p;
  13. double d;
  14. char note[15];
  15. }item;
  16. item item1[100], item2[100];
  17.  
  18. float knapsack;
  19. int bnyk, k[100], n[100];
  20. double o[100];
  21. float l[100], m[100];
  22.  
  23. int main()
  24. {
  25.  
  26. int X[100];
  27.  
  28.  
  29. printf("capacity : "); scanf("%f", &knapsack);
  30. printf("sum of variation : "); scanf("%d", &bnyk);
  31. printf("\n");
  32. Input_Data(X,bnyk);
  33. Show_Data(X,bnyk);
  34. int i, j, c;
  35.  
  36.  
  37. float totwd, totpd;
  38. c=0, totwd=0, totpd=0;
  39. for(i=0; i<bnyk; i++)
  40. for(j=0; j<item1[i].b; j++)
  41. {
  42. k[c]=item1[i].no;
  43. l[c]=item1[i].w;
  44. m[c]=item1[i].p;
  45. n[c]=item1[i].b;
  46. o[c]=item1[i].d;
  47. c++;
  48. }
  49. Sort_Density(k,l,m,n,o,c);
  50. for(i=0; i<c; i++)
  51. {
  52. item2[i].no=k[i];
  53. item2[i].w=l[i];
  54. item2[i].p=m[i];
  55. item2[i].b=n[i];
  56. item2[i].d=o[i];
  57. }
  58. printf("SORT DATA BY DENSITY\n");
  59. printf("Objek weight profit density \n");
  60. printf("--------------------------------------------------\n");
  61. for(i=0; i<c; i++)
  62. {
  63. printf("%d %f %f %f\n", item2[i].no, item2[i].w, item2[i].p, item2[i].d);
  64. }
  65.  
  66.  
  67.  
  68. }
  69. void Input_Data(int item[], int jum)
  70. {
  71. int i;
  72. for(i=0; i<jum; i++)
  73. {
  74. printf("w%d : ", i+1); scanf("%f", &item1[i].w);
  75. printf("p%d : ", i+1); scanf("%f", &item1[i].p);
  76. printf("tot%d : ", i+1); scanf("%d", &item1[i].b);
  77. item1[i].no=i+1;
  78. }
  79. }
  80.  
  81. void Show_Data(int item[], int jum)
  82. {
  83. int i;
  84. printf("\n");
  85. printf("Objek(i) weight(w) Profit(p) pieces(b) Density(d)\n");
  86. printf("-------------------------------------------------------------\n");
  87. for(i=0; i<jum; i++)
  88. {
  89. item1[i].d=item1[i].p/item1[i].w;
  90. printf("%2d %12f %12f %12d %12f\n", item1[i].no, item1[i].w, item1[i].p, item1[i].b, item1[i].d);
  91. }
  92. }
  93.  
  94. void Sort_Density(int *no, float *weight, float *profit, int *pieces, double *density, int c)
  95. {
  96. int i, j;
  97. double temp;
  98. for(i=0; i<c-1; i++)
  99. for(j=i; j<c; j++)
  100. {
  101. if(density[i]<density[j])
  102. {
  103. temp=no[i];
  104. no[i]=no[j];
  105. no[j]=temp;
  106. temp=weight[i];
  107. weight[i]=weight[j];
  108. weight[j]=temp;
  109. temp=profit[i];
  110. profit[i]=profit[j];
  111. profit[j]=temp;
  112. temp=pieces[i];
  113. pieces[i]=pieces[j];
  114. pieces[j]=temp;
  115. temp=density[i];
  116. density[i]=density[j];
  117. density[j]=temp;
  118. }
  119. }
  120. }
Success #stdin #stdout 0s 3308KB
stdin
2000
4
300
350
3
400
400
3
250
500
4
250
360
7
stdout
capacity : sum of variation : 
w1 : p1 : tot1 : w2 : p2 : tot2 : w3 : p3 : tot3 : w4 : p4 : tot4 : 
Objek(i)    weight(w)     Profit(p)    pieces(b)    Density(d)
-------------------------------------------------------------
 1   300.000000   350.000000            3     1.166667
 2   400.000000   400.000000            3     1.000000
 3   250.000000   500.000000            4     2.000000
 4   250.000000   360.000000            7     1.440000
SORT DATA BY DENSITY
Objek     weight     profit      density 
--------------------------------------------------
3  250.000000 500.000000 2.000000
3  250.000000 500.000000 2.000000
3  250.000000 500.000000 2.000000
3  250.000000 500.000000 2.000000
4  250.000000 360.000000 1.440000
4  250.000000 360.000000 1.440000
4  250.000000 360.000000 1.440000
4  250.000000 360.000000 1.440000
4  250.000000 360.000000 1.440000
4  250.000000 360.000000 1.440000
4  250.000000 360.000000 1.440000
1  300.000000 350.000000 1.166667
1  300.000000 350.000000 1.166667
1  300.000000 350.000000 1.166667
2  400.000000 400.000000 1.000000
2  400.000000 400.000000 1.000000
2  400.000000 400.000000 1.000000