Задача 20 Урок 15

Задача 20 Урок 15

Задача про банкомат:

program lesson15_20;
type
    myArr = array[1..4] of integer;

procedure atm(c: integer; a: myArr; var b: myArr; var d: boolean);
var
    i, temp: integer;
begin
    for i := low(a) to high(a) do
    begin
        temp := c div a[i];
        b[i] := temp;
        c := c - temp * a[i];
	end;
    if c > 0 then
        d := false;
end;

var
    arr1: myArr = (1000, 500, 100, 50);
    arr2: myArr;
    c: boolean = true;
    a, i: integer;
begin
    writeln('enter number:');
    readln(a);

    for i := low(arr2) to high(arr2) do
        arr2[i] := 0;

    atm(a, arr1, arr2, c);

    if c then
    begin
        for i := low(arr2) to high(arr2) do
            if arr2[i] > 0 then
                write(arr2[i], ' * ', arr1[i], ' | ');
    end
	else
        writeln('there is no exchange');

    readln();
end.
vedro-compota's picture

Пусть есть произвольный набор 5 купюр - написать решение для этого случая

Пример:

500 200 100 50 30

Выдать сумму:

120

_____________
матфак вгу и остальная классика =)

program lesson15_20;
type
    myArr = array[1..5] of integer;

procedure atm(num: integer; a: myArr; var a2: myArr; var b: boolean);
var
    k1, k2, k3, k4, k5: integer;
    i, j, k, l, n: integer;
begin

    k1 := num div a[1];
    k2 := num div a[2];
    k3 := num div a[3];
    k4 := num div a[4];
    k5 := num div a[5];

    for i := k1 downto 0 do
        for j := k2 downto 0 do
            for k := k3 downto 0 do
                for l := k4 downto 0 do
                    for n := k5 downto 0 do
                        if i*a[1]+j*a[2]+k*a[3]+l*a[4]+n*a[5]=num then
                        begin
                            b := true;
                            a2[1] := i;
                            a2[2] := j;
                            a2[3] := k;
                            a2[4] := l;
                            a2[5] := n;
                            exit;
                        end;
end;

var
    arr1: myArr = (700, 330, 180, 45, 7);
    arr2: myArr;
    b: boolean = false;
    a, i: integer;
begin
    writeln('enter number:');
    readln(a);

    for i := low(arr2) to high(arr2) do
        arr2[i] := 0;

    atm(a, arr1, arr2, b);

    if b then
        for i := low(arr2) to high(arr2) do
            write(arr2[i], ' ')
	else
        writeln('there is no exchange');

    readln();
end.
vedro-compota's picture

решение засчитано

_____________
матфак вгу и остальная классика =)