英文:
How to distribute array elements into another array while no array has more than 1 element that the other?
问题
我需要将 x 个学生从一个数组分配并添加到 n 个数组中,但每个数组不能多于另一个数组中的 1 个学生,例如:
将 17 名学生分成 3 个数组,
前两个数组将容纳 6 名学生,最后一个数组将有 5 名学生。
我尝试过以下方法,但结果不一致:
double x = 17;
double n = 3;
double theSizeOfEachArray = x % n;
String[] array1;
String[] array2;
String[] array3;
if (theSizeOfEachArray == 0) {
array1 = new String[(int) (x / n)];
array2 = new String[(int) (x / n)];
array3 = new String[(int) (x / n)];
}
if (theSizeOfEachArray == 1) {
array1 = new String[(int) Math.ceil(x / n)];
array2 = new String[(int) Math.floor(x / n)];
array3 = new String[(int) Math.floor(x / n)];
}
if (theSizeOfEachArray == 2) {
array1 = new String[(int) Math.ceil(x / n)];
array2 = new String[(int) Math.ceil(x / n)];
array3 = new String[(int) Math.floor(x / n)];
}
// ...
这只适用于数组数量为 3 的情况。
英文:
i need to distribute x number of students from an array and add them to n number of array but no array can have more than 1 from the other for example:
17 students into 3 arrays
the first two arrays will hold 6 and the last one will have 5
i tried to do this but it is not consistent :
double x = 17;
double n = 3;
double theSizeOfEachArray = x%n;
String[] array1;
String[] array2;
String[] array3;
if (theSizeOfEachArray ==0){
array1 =new String[x/n];
array2 =new String[x/n];
array3 =new String[x/n];
}
if (theSizeOfEachArray ==1)
array1 =new String[Math.ceil(x/n)];
array2 =new String[Math.floor(x/n)];
array3 =new String[Math.floor(x/n)];
if (theSizeOfEachArray ==2)
array1 =new String[Math.ceil(x/n)];
array2 =new String[Math.ceil(x/n)];
array3 =new String[Math.floor(x/n)];
....
it will only work if the number of arrays are 3
答案1
得分: 0
尝试类似这样的写法:
int x = 17;
int n = 3;
int theSizeOfEachArray = x%n;
String[] array1 = new String[x / n + ((theSizeOfEachArray > 0) ? 1 : 0)];
if(theSizeOfEachArray > 0)
theSizeOfEachArray--;
String[] array2 = new String[x / n + ((theSizeOfEachArray > 0) ? 1 : 0)];
if(theSizeOfEachArray > 0)
theSizeOfEachArray--;
String[] array3 = new String[x / n + ((theSizeOfEachArray > 0) ? 1 : 0)];
我改用了整数而不是浮点数,因为处理起来更方便。
基本上,在每次数组初始化时,我都会检查是否还有余数,如果有的话,就加1。然后如果必要的话,我会递减余数。在这种特定情况下,我甚至可以将数组初始化和递减的操作压缩成一行代码:
int x = 17;
int n = 3;
int theSizeOfEachArray = x%n;
String[] array1 = new String[x / n + ((theSizeOfEachArray-- > 0) ? 1 : 0)];
String[] array2 = new String[x / n + ((theSizeOfEachArray-- > 0) ? 1 : 0)];
String[] array3 = new String[x / n + ((theSizeOfEachArray-- > 0) ? 1 : 0)];
((theSizeOfEachArray-- > 0) ? 1 : 0)
是一个三元表达式。基本上,如果条件为真,那么在“?”和“:”之间的“1”将被返回。否则,返回“0”。
英文:
Try something like this:
int x = 17;
int n = 3;
int theSizeOfEachArray = x%n;
String[] array1 = new String[x / n + ((theSizeOfEachArray > 0) ? 1 : 0)];
if(theSizeOfEachArray > 0)
theSizeOfEachArray--;
String[] array2 = new String[x / n + ((theSizeOfEachArray > 0) ? 1 : 0)];
if(theSizeOfEachArray > 0)
theSizeOfEachArray--;
String[] array3 = new String[x / n + ((theSizeOfEachArray > 0) ? 1 : 0)];
I switched to using ints instead of doubles because they are easier to deal with.
Basically, on each array initialization, I check to see if there is still a remainder and if there is, add 1. Then I decrement the remainder if necessary. In this particular case, I can even condense the array initialization and decrement into one line of code:
int x = 17;
int n = 3;
int theSizeOfEachArray = x%n;
String[] array1 = new String[x / n + ((theSizeOfEachArray-- > 0) ? 1 : 0)];
String[] array2 = new String[x / n + ((theSizeOfEachArray-- > 0) ? 1 : 0)];
String[] array3 = new String[x / n + ((theSizeOfEachArray-- > 0) ? 1 : 0)];
((theSizeOfEachArray-- > 0) ? 1 : 0)
is a ternary expression. Basically, if the condition is true, then the "1" between "?" and ":" is returned. Otherwise, the "0" is.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论