Untitled
raw download clone
C
views 12
,
size 1569 b
#include <stdio.h>
#define MAXNV 5
int DONE = 0;
int liters[MAXNV];
int numbers[MAXNV];
void show(int nv);
void filling(int amount, int cur, int nv);
int j=0;
int water;
int min=100;
int min_numbers[MAXNV]={0};
int main(void)
{
    int nv, i;

    scanf("%d", &nv);
    for (i=0; i<nv; i++) {
        scanf("%d", &liters[i]);
    }
    scanf("%d", &water);
    filling(water, 0, nv);

    for(i=0;i<nv;i++)
        numbers[i]=min_numbers[i];
    show(nv);
    return 0;
}

void show(int nv)
{
    int i;
    printf("(%d", numbers[0]);
    for (i=1; i<nv; i++) {
        printf(",%d", numbers[i]);
    }
    printf(")\n");
}

void filling(int amount, int cur, int nv)
{
    if (cur == nv) {
        if (amount == 0){
            int sum = 0;
            for (int i = 0;i < nv;i++) sum += numbers[i];
            if (sum < min)
            {
                min = sum;
                for (int i = 0;i < nv;i++)
                {
                    min_numbers[i] = numbers[i];
                }
            }
        }
    }
    else if (amount < 0) {}
    else if (amount == 0) {
        int sum = 0;
        for (int i = 0;i < nv;i++) sum += numbers[i];
        if (sum < min)
        {
            min = sum;
            for (int i = 0;i < nv;i++)
            {
                min_numbers[i] = numbers[i];
            }
        }
    }
    else {
        filling(amount, cur + 1, nv);

        numbers[cur]++;
        filling(amount - liters[cur], cur, nv);
        numbers[cur]--;
    }
}
close fullscreen
Login or Register to edit or fork this paste. It's free.