Home > Back-end >  PAT 1020 cakes (25 points) using C language implementation, please help to find the problem in where
PAT 1020 cakes (25 points) using C language implementation, please help to find the problem in where

Time:12-22

title:
The Chinese during the Mid-Autumn festival to eat moon cakes are a kind of traditional food, in different areas, there are many different flavors of moon cakes, now given all kinds of mooncakes inventories, total price, and the biggest market demand, can you please calculate how much is the biggest gain,

Note: sales allowed to remove part of the inventory, when given the sample situation is this: if we have 3 kinds of moon cakes, the inventory of 18,15,10 respectively ten thousand tons, a total of 75,72,45 respectively, one hundred million yuan, if the market demand for the biggest only 200000 tons, so all our biggest revenue strategy should be to sell 150000 tons of 2 kinds of moon cakes, and 50000 tons of 3 kinds of moon cakes, 72 + 45/2=94.5 (one hundred million yuan),

Input format:
Each input contains a test cases, each test case gives a positive integer less than 1000 N according to the type of number, and no more than 500 (ten thousand tons) of positive integer D according to market demand, biggest then a line N given positive said inventory for each moon cake (ten thousand tons); Last line give N positive said the total price of each moon cake (with one hundred million yuan as the unit), separated by Spaces between Numbers,

The output format:
For each set of test cases, output maximum benefits in one line, with one hundred million yuan as the unit and accurate to 2 decimal places,

Input the sample:
3 20
18 15 10
75 72 45
The output sample:
94.50

I C code:
 # include & lt; stdio.h> 

Int main (void) {
Int N, D, I, j;
Double stock [1000], price [1000], the unit [1000], the sum=0.0, the balance=0.0;//unit is the unit price

//input data
The scanf (" % d % d ", & amp; N, & amp; D);
//getchar ();
for(i=0; iLf the scanf (" % ", & amp; Stock [I]);
}
for(i=0; iLf the scanf (" % ", & amp; Price [I]);
Unit [I]=price/stock [I] [I];
}

//all kinds of mooncakes inventory stock, total price, according to the unit price from big to small order
Double temp.
for(i=0; ifor(j=1; jIf (unit [I] Temp=unit [I]; The unit [I]=unit [j]; The unit [j]=temp;
Temp=price [I]; Price [I]=price [j]; Price [j]=temp;
Temp=stock [I]; Stock [I]=stock [j]; Stock [j]=temp;
}
}
}

//the sum calculated maximum return
The balance D=(double);
for(i=0; iIf (balance<=stock [I]) {
The sum price +=the balance * [I]/stock [I];
break;
} else {
The balance -=stock [I];
The sum +=price [I];
}
}
Printf (" % 2 lf \ n ", sum);

return 0;
}


Results the
(post for the first time, the picture is not in, can only use text copy the ) :
Test scores point results take memory
0 answers right 13 3 ms 320 KB
1 answer wrong 0 6 ms 188 KB
2 the answer correct 2, 3, ms 196 KB
3 the answer correct 2 4 ms 188 KB
4 the answer correct 2 4 ms 200 KB

The first test point has been wrong, don't know where is wrong, the great god, please help to check, thank you ~

CodePudding user response:

Sort of two for write wrong, look carefully,

CodePudding user response:

reference 1st floor forever74 response:
sort of two for write wrong, look carefully,

Checked several times, also did not find where wrong, help a look oh, thank you

CodePudding user response:

For (j=I + 1; .
  • Related