There four sides of a wall(a square), the cost of building this wall:
1 side(length) $1200 per meter
2 side(width) $2500 per meter
1 side(length) $2500 per meter
the max of length & width are both 100
you have limited budget $20000 to best use to build the wall, the accuracy of length and width should be down to 0.1 meter, use two for loops to find out the best combination of length & width.
here is my code (wrong)
Code:
#include <stdio.h>
struct dimension {
float length; /* river side length */
float width;
};
void maxSize(float amount, struct dimension* dim) {
/* Enter your code here */
float i, j;
float max_area = 0;
if ((2500 * 300 + 1200 * 100 ) < amount) {
dim->length = 100;
dim->width = 100;
return;
}
//length = i //width = j
for (i=100; i>=0.1; i-=0.1)
for (j=100; j>=0.1; j-=0.1)
area = i*j;
cost = i*1200 + (i+j*2) * 2500;
if( cost < amount && area < max_area)
break;
}
int main() {
struct dimension dim;
maxSize(20000, &dim);
}