There are 3 recycling boxes; each box contains various combinations of Red, Whit
ID: 3861696 • Letter: T
Question
There are 3 recycling boxes; each box contains various combinations of Red, White, and Blue bottles. You may assume the max. # of bottles in each box is < 3000.
Example.
Write a C-program that will prompt user input of the various combinations of bottles in the three boxes. Your program will then calculate the least number of moves that will sort the bottles into their respective boxes. It doesn’t matter which box you use to hold which colour bottles. You are only required to output the least number of moves to sort them. For example, using the data in the above table, your output should be: The least number of moves required to sort these boxes is 622.
Box 'C' Box B OX Red Bottles 100 RedBottles 22 Red Bottles 150 White Bottles 25 WhiteBottles 300 WhiteBottles -275 BlueBottles 169 Blue Bottles 200 Blue Bottles 0Explanation / Answer
C code:
#include <stdio.h>
int main()
{
int ra, rb,rc, wa,wb,wc, ba,bb,bc;
printf("Enter number of Red bottles in A ");
scanf("%i",&ra);
printf("Enter number of white bottles in A ");
scanf("%i",&wa);
printf("Enter number of Blue bottles in A ");
scanf("%i",&ba);
printf("Enter number of Red bottles in B ");
scanf("%i",&rb);
printf("Enter number of white bottles in B ");
scanf("%i",&wb);
printf("Enter number of Blue bottles in B ");
scanf("%i",&bb);
printf("Enter number of Red bottles in C ");
scanf("%i",&rc);
printf("Enter number of white bottles in C ");
scanf("%i",&wc);
printf("Enter number of Blue bottles in C ");
scanf("%i",&bc);
int cost = 0;
// case1 A->R B->W C->B
cost = rb + rc + wa + wc + ba + bb;
// case2 A->R B->B C->W
int tmp = rb + rc + ba + bc + wa + wb;
if(cost > tmp)
{
cost = tmp;
}
// case3 A->W B->R C->B
tmp = wb + wc + ra + rc + ba + bb;
if(cost > tmp)
{
cost = tmp;
}
// case4 A->W B->B C->R
tmp = wb + wc + ba + bc + ra + rb;
if(cost > tmp)
{
cost = tmp;
}
// case5 A->B B->W C->R
tmp = bb + bc + wa + wc + ra + rb;
if(cost > tmp)
{
cost = tmp;
}
// case6 A->B B->R C->W
tmp = bb + bc + ra + rc + wa + wb;
if(cost > tmp)
{
cost = tmp;
}
printf( "Minimum number of moves required to sort these boxes is %i ", cost);
}
Sample Output:
Enter number of Red bottles in A
100
Enter number of white bottles in A
25
Enter number of Blue bottles in A
169
Enter number of Red bottles in B
22
Enter number of white bottles in B
300
Enter number of Blue bottles in B
200
Enter number of Red bottles in C
150
Enter number of white bottles in C
275
Enter number of Blue bottles in C
0
Minimum number of moves required to sort these boxes is 622
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.