C language: I want to make sure that if the result is duplicated, it is only the minimum box

Asked 2 years ago, Updated 2 years ago, 139 views

It is a problem to put 12~17kg of sugar in boxes of 3KG and 5KG so that the weight of the box is minimal without leaving any weight.

I want to make it so that if it comes out in duplicate in the code I wrote, it only comes out in the minimum box, but I don't know what sentence to add.

#include<stdio.h>
int main(void) {

    inti, box_5, box_3; // repeat variable, 5kg box count, 3kg box count

    For (i = 12; i <= 17; i++) // Repeat in the number range with the sugar key. 
    {
        For (box_5 = 0; box_5 <= 3; box_5++) // Repeat within the range of the number of boxes (0 - 3 = > 0 - 15 kg). 
        {
            For (box_3 = 0; box_3 <= 5; box_3++) // Repeat within the range of the number of boxes (0 to 5 = 0 to 15 kg).
            {
                If (i == 5 * box_5 + 3 * box_3) // Total number of sugar keys = 5 * 5 kg boxes + 3 * 3 kg boxes.
                {
                    printf("%dkg: 5kg%dbox, 3kg%dbox\n", i, box_5, box_3);

                }
            }
        }


    }

    return 0;

c loops for

2022-09-20 16:34

1 Answers

The triplet is too heavy.

#include<stdio.h>
int main(void) {

    inti, box_5, box_3; // repeat variable, 5kg box count, 3kg box count

    For (i = 12; i <= 17; i++) // Repeat in the number range with the sugar key. 
    {
        For (box_5 = 0; box_5 <= 3; box_5++) // Repeat within the range of the number of boxes (0 - 3 = > 0 - 15 kg). 
        {
            For (box_3 = 0; box_3 <= 5; box_3++) // Repeat within the range of the number of boxes (0 to 5 = 0 to 15 kg).
            {
                If (i == 5 * box_5 + 3 * box_3) // Total number of sugar keys = 5 * 5 kg boxes + 3 * 3 kg boxes.
                {
                    printf("%dkg: 5kg%dbox, 3kg%dbox\n", i, box_5, box_3);
                                        break;

                }
            }
        }


    }

    return 0;
}


2022-09-20 16:34

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.