#include<cstdio>
#include<cstring>
#include<cmath>
void Input_Data(int item[], int jum);
void Show_Data(int item[], int jum);
void Sort_Density(int *no, float *weight, float *profit, int *pieces, double *density, int c);
typedef struct
{
int b, no;
float w, p;
double d;
char note[15];
}item;
item item1[100], item2[100];
float knapsack;
int bnyk, k[100], n[100];
double o[100];
float l[100], m[100];
int main()
{
int X[100];
printf("capacity : "); scanf("%f", &knapsack);
printf("sum of variation : "); scanf("%d", &bnyk);
printf("\n");
Input_Data(X,bnyk);
Show_Data(X,bnyk);
int i, j, c;
float totwd, totpd;
c=0, totwd=0, totpd=0;
for(i=0; i<bnyk; i++)
for(j=0; j<item1[i].b; j++)
{
k[c]=item1[i].no;
l[c]=item1[i].w;
m[c]=item1[i].p;
n[c]=item1[i].b;
o[c]=item1[i].d;
c++;
}
Sort_Density(k,l,m,n,o,c);
for(i=0; i<c; i++)
{
item2[i].no=k[i];
item2[i].w=l[i];
item2[i].p=m[i];
item2[i].b=n[i];
item2[i].d=o[i];
}
printf("SORT DATA BY DENSITY\n");
printf("Objek weight profit density \n");
printf("--------------------------------------------------\n");
for(i=0; i<c; i++)
{
printf("%d %f %f %f\n", item2[i].no, item2[i].w, item2[i].p, item2[i].d);
}
}
void Input_Data(int item[], int jum)
{
int i;
for(i=0; i<jum; i++)
{
printf("w%d : ", i+1); scanf("%f", &item1[i].w);
printf("p%d : ", i+1); scanf("%f", &item1[i].p);
printf("tot%d : ", i+1); scanf("%d", &item1[i].b);
item1[i].no=i+1;
}
}
void Show_Data(int item[], int jum)
{
int i;
printf("\n");
printf("Objek(i) weight(w) Profit(p) pieces(b) Density(d)\n");
printf("-------------------------------------------------------------\n");
for(i=0; i<jum; i++)
{
item1[i].d=item1[i].p/item1[i].w;
printf("%2d %12f %12f %12d %12f\n", item1[i].no, item1[i].w, item1[i].p, item1[i].b, item1[i].d);
}
}
void Sort_Density(int *no, float *weight, float *profit, int *pieces, double *density, int c)
{
int i, j;
double temp;
for(i=0; i<c-1; i++)
for(j=i; j<c; j++)
{
if(density[i]<density[j])
{
temp=no[i];
no[i]=no[j];
no[j]=temp;
temp=weight[i];
weight[i]=weight[j];
weight[j]=temp;
temp=profit[i];
profit[i]=profit[j];
profit[j]=temp;
temp=pieces[i];
pieces[i]=pieces[j];
pieces[j]=temp;
temp=density[i];
density[i]=density[j];
density[j]=temp;
}
}
}
I2luY2x1ZGU8Y3N0ZGlvPgojaW5jbHVkZTxjc3RyaW5nPgojaW5jbHVkZTxjbWF0aD4KCnZvaWQgSW5wdXRfRGF0YShpbnQgaXRlbVtdLCBpbnQganVtKTsKdm9pZCBTaG93X0RhdGEoaW50IGl0ZW1bXSwgaW50IGp1bSk7CnZvaWQgU29ydF9EZW5zaXR5KGludCAqbm8sIGZsb2F0ICp3ZWlnaHQsIGZsb2F0ICpwcm9maXQsIGludCAqcGllY2VzLCBkb3VibGUgKmRlbnNpdHksIGludCBjKTsKCnR5cGVkZWYgc3RydWN0CnsKICAgIGludCBiLCBubzsKICAgIGZsb2F0IHcsIHA7CiAgICBkb3VibGUgZDsKICAgIGNoYXIgbm90ZVsxNV07Cn1pdGVtOwppdGVtIGl0ZW0xWzEwMF0sIGl0ZW0yWzEwMF07CgpmbG9hdCBrbmFwc2FjazsKaW50IGJueWssIGtbMTAwXSwgblsxMDBdOwpkb3VibGUgb1sxMDBdOwpmbG9hdCBsWzEwMF0sIG1bMTAwXTsKCmludCBtYWluKCkKewoKICAgIGludCBYWzEwMF07CgoKICAgIHByaW50ZigiY2FwYWNpdHkgOiAiKTsgc2NhbmYoIiVmIiwgJmtuYXBzYWNrKTsKICAgIHByaW50Zigic3VtIG9mIHZhcmlhdGlvbiA6ICIpOyBzY2FuZigiJWQiLCAmYm55ayk7CiAgICBwcmludGYoIlxuIik7CiAgICBJbnB1dF9EYXRhKFgsYm55ayk7CiAgICBTaG93X0RhdGEoWCxibnlrKTsKICAgIGludCBpLCBqLCBjOwoKCiAgICBmbG9hdCB0b3R3ZCwgdG90cGQ7CiAgICBjPTAsIHRvdHdkPTAsIHRvdHBkPTA7CiAgICBmb3IoaT0wOyBpPGJueWs7IGkrKykKICAgIGZvcihqPTA7IGo8aXRlbTFbaV0uYjsgaisrKQogICAgewogICAgICAgIGtbY109aXRlbTFbaV0ubm87CiAgICAgICAgbFtjXT1pdGVtMVtpXS53OwogICAgICAgIG1bY109aXRlbTFbaV0ucDsKICAgICAgICBuW2NdPWl0ZW0xW2ldLmI7CiAgICAgICAgb1tjXT1pdGVtMVtpXS5kOwogICAgICAgIGMrKzsKICAgIH0KICAgIFNvcnRfRGVuc2l0eShrLGwsbSxuLG8sYyk7CiAgICBmb3IoaT0wOyBpPGM7IGkrKykKICAgIHsKICAgICAgICBpdGVtMltpXS5ubz1rW2ldOwogICAgICAgIGl0ZW0yW2ldLnc9bFtpXTsKICAgICAgICBpdGVtMltpXS5wPW1baV07CiAgICAgICAgaXRlbTJbaV0uYj1uW2ldOwogICAgICAgIGl0ZW0yW2ldLmQ9b1tpXTsKICAgIH0KICAgIHByaW50ZigiU09SVCBEQVRBIEJZIERFTlNJVFlcbiIpOwogICAgcHJpbnRmKCJPYmplayAgICAgd2VpZ2h0ICAgICBwcm9maXQgICAgICBkZW5zaXR5IFxuIik7CiAgICBwcmludGYoIi0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tXG4iKTsKICAgIGZvcihpPTA7IGk8YzsgaSsrKQogICAgewogICAgICAgIHByaW50ZigiJWQgICVmICVmICVmXG4iLCBpdGVtMltpXS5ubywgaXRlbTJbaV0udywgaXRlbTJbaV0ucCwgaXRlbTJbaV0uZCk7CiAgICB9CgoKCn0Kdm9pZCBJbnB1dF9EYXRhKGludCBpdGVtW10sIGludCBqdW0pCnsKICAgIGludCBpOwogICAgZm9yKGk9MDsgaTxqdW07IGkrKykKICAgIHsKICAgICAgICBwcmludGYoInclZCA6ICIsIGkrMSk7IHNjYW5mKCIlZiIsICZpdGVtMVtpXS53KTsKICAgICAgIC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gICAgbm9baV09bm9bal07CiAgICAgICAgICAgICAgICBub1tqXT10ZW1wOwogICAgICAgICAgICAgICAgdGVtcD13ZWlnaHRbaV07CiAgICAgICAgICAgICAgICB3ZWlnaHRbaV09d2VpZ2h0W2pdOwogICAgICAgICAgICAgICAgd2VpZ2h0W2pdPXRlbXA7CiAgICAgICAgICAgICAgICB0ZW1wPXByb2ZpdFtpXTsKICAgICAgICAgICAgICAgIHByb2ZpdFtpXT1wcm9maXRbal07CiAgICAgICAgICAgICAgICBwcm9maXRbal09dGVtcDsKICAgICAgICAgICAgICAgIHRlbXA9cGllY2VzW2ldOwogICAgICAgICAgICAgICAgcGllY2VzW2ldPXBpZWNlc1tqXTsKICAgICAgICAgICAgICAgIHBpZWNlc1tqXT10ZW1wOwogICAgICAgICAgICAgICAgdGVtcD1kZW5zaXR5W2ldOwogICAgICAgICAgICAgICAgZGVuc2l0eVtpXT1kZW5zaXR5W2pdOwogICAgICAgICAgICAgICAgZGVuc2l0eVtqXT10ZW1wOwogICAgICAgICAgICB9CiAgICAgICAgfQp9