-2

В вузе задали задачу на java, подскажите как её решить? Или может какие-нибудь обучающие материалы о том, как решать подобные. Далее текст на английском:

Define a three-dimensional array of ints representing deposits and withdrawals of customers of a bank in and out of their accounts (each customer may have several accounts). For example:

int[][][] opers = {
        {{100, -50, 25}, {150, -300}, {300, -90, 100}},
        {{90, -60, 250}, {300, 20, -100}},
        {{20, 50}, {300}, {20, -20, 40}, {100, -200}}
};
  • the first index indicates a customer;
  • the second index indicates, for a given customer, his/her account;
  • the third index indicates, for a given customer and his/her account, subsequent deposits (positive values) and withdrawals (negative values).

The program should create an array of ints of dimension equal to the number of customers, the elements of which are sums of all deposits and withdrawals for subsequent customers, in and out of all his/her accounts (for the data as in the example above, these should be the numbers 235, 500 and 310).

Leonid
  • 3
  • в чем у вас сложности? Если проблема с созданием массива, то любая книга по java для начинающих вам подойдет. – tym32167 Dec 04 '19 at 13:53
  • Не могу точно понять какой массив требуется создать – Leonid Dec 04 '19 at 14:02
  • 1
    Мне кажется, это лучше спросить у препода, если массива из примера не достаточно. – tym32167 Dec 04 '19 at 14:15

2 Answers2

0

Тут же очень просто все. Массив нужно создать по количеству клиентов (для данного примера итоговый массив должен быть длиной 3). А вообще:

int[] accounts = new int[opers.length];

Затем в циклах просуммировать с накоплением все числа по каждому клиенту.

accounts[i] += opers[i][j][k];

Надеюсь, этой подсказки должно быть достаточно, чтобы решить задачу.

Alice Magic
  • 1,066
0

Суммируем второе и третье измерение трехмерного массива - получаем одномерный массив:

int[][][] opers = {
        {{100, -50, 25}, {150, -300}, {300, -90, 100}},
        {{90, -60, 250}, {300, 20, -100}},
        {{20, 50}, {300}, {20, -20, 40}, {100, -200}}
};

int[] customers = Arrays.stream(opers) .mapToInt(arr -> Arrays.stream(arr) .mapToInt(row -> Arrays.stream(row).sum()) .sum()) .toArray();

System.out.println(Arrays.toString(customers)); // [235, 500, 310]