Skip to content
Snippets Groups Projects
Commit 4eb78cdf authored by iliya's avatar iliya
Browse files

feat: ex3 serie2 finished

parent 9a689be0
No related branches found
No related tags found
No related merge requests found
*.class
public class ContainerHelper {
public static void ensurePositiveValue(int val) {
if (val < 0) {
throw new RuntimeException("Value should be greater than or equal to zero\tCurrent value: " + val);
}
}
public static void ensurePositiveValue(int... vals) {
for (int i : vals) {
ensurePositiveValue(i);
}
}
public static int[] transfer(int srcQuantity, int dest, int... dests) {
ensurePositiveValue(srcQuantity);
ensurePositiveValue(dest);
ensurePositiveValue(dests);
int[] res = new int[dests.length + 1];
int quantityToTransferDest = Math.min(srcQuantity, dest);
res[0] = quantityToTransferDest;
srcQuantity -= quantityToTransferDest;
for (int i = 1; i < res.length; i++) {
if (srcQuantity > 0) {
int quantityToTransferDests = Math.min(srcQuantity, dests[i - 1]);
res[i] = quantityToTransferDests;
srcQuantity -= quantityToTransferDests;
}
}
return res;
}
public static void main(String[] args) {
int src = 100;
int dest = 10;
int arg1 = 40;
int arg2 = 30;
int arg3 = 50;
int res[] = transfer(src, dest, arg1, arg2, arg3);
System.out.print("{");
for (int i = 0; i < res.length; i++) {
System.out.print(res[i]);
if (i < res.length - 1) {
System.out.print(", ");
} else {
System.out.println("}");
}
}
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment