英文:
Need help altering modulus equation
问题
我目前需要生成一个由以下元素组成的数组:
-0.5, 0, 0.5, 0, -0.5, 0, 0.5, 0...
等等。这是我目前的代码:
// Sequence: -0.5, 0, 0.5, 0, -0.5, 0, 0.5...
public static double[] generateI(int n) {
if (n < 0) {
return null;
} else if (n == 0) {
return new double[0];
}
double[] arr = new double[n];
for (int i = 0; i < n; i++) {
arr[i] = ((0.5 * i) % 2) - 0.5; //需要在这里提供帮助。
}
return arr;
}
然而,我的输出是:
[-0.5, 0.0, 0.5, **1.0**, -0.5, 0.0, 0.5, **1.0**, -0.5, 0.0, 0.5, **1.0**]
。
因此,我的数学方程是错误的。
(注意,我尝试过使用-Math.cos(i * Math.PI/2)/2
方程,虽然这是一个有效的解决方案,但我被要求使用单行的取模函数来完成。)
另一个序列(供感兴趣的人使用)是:
-0, 1, 2, 3, 1, 2, 3, 4, 2, 3, 4, 5...
感谢任何帮助!
英文:
I currently need to produce an array consisting of the following elements:
-0.5, 0, 0.5, 0, -0.5, 0, 0.5, 0...
and so on. This is my current code:
// Sequence: -0.5, 0, 0.5, 0, -0.5, 0, 0.5...
public static double[] generateI(int n) {
if (n < 0) {
return null;
} else if (n == 0) {
return new double[0];
}
double[] arr = new double[n];
for (int i = 0; i < n; i++) {
arr[i] = ((0.5 * i) % 2) - 0.5; \\need help here.
}
return arr;
}
However, my output is:
[-0.5, 0.0, 0.5, 1.0, -0.5, 0.0, 0.5, 1.0, -0.5, 0.0, 0.5, 1.0].
Hence, my math equation is wrong.
(note that I have tried using the -Math.cos(i * Math.PI/2)/2
equation, and although it is a valid solution, I was tasked to do it using a modulus function in a single line of code.)
Another sequence (for those interested) is:
-0, 1, 2, 3, 1, 2, 3, 4, 2, 3, 4, 5...
Any help is appreciated!
答案1
得分: 2
使用 otherSequence
标志来更改您要激活的两个模式中的哪一个。
对于您提到的第二个模式,您需要对 i
和 4
取模(以返回 [0, 1, 2, 3]
),然后将 i
与 4
进行整数除法的结果相加,以使序列继续到下一个 4 个数字(例如 i = 6
(6 / 4 = 1 -> [1, 2, 3, 4]
)。
class Main {
public static double[] generateI(int n, boolean other_sequence) {
if (n < 0) {
return null;
} else if (n == 0) {
return new double[0];
}
double[] arr = new double[n];
if (other_sequence) {
Arrays.setAll(arr, i -> ((i % 4) + (i / 4)));
} else {
Arrays.setAll(arr, i -> ((i % 4) - 1) * ((i + 1) % 2) * 0.5);
}
return arr;
}
public static void main(String[] args) {
System.out.println(Arrays.toString(generateI(8, false)));
System.out.println(Arrays.toString(generateI(16, true)));
}
}
输出:
[-0.5, 0.0, 0.5, 0.0, -0.5, 0.0, 0.5, 0.0]
[0.0, 1.0, 2.0, 3.0, 1.0, 2.0, 3.0, 4.0, 2.0, 3.0, 4.0, 5.0, 3.0, 4.0, 5.0, 6.0]
英文:
Use the otherSequence
flag to change which of your two patterns you want to activate.
For the second pattern you've mentioned, you need to take the mod of i
and 4
(to return [0, 1, 2, 3]
) and then add the result of the integer division between i
and 4
in order to make the sequence continue to the next 4 numbers (e.g. i = 6
(6 / 4 = 1 -> [1, 2, 3, 4]
)
class Main {
public static double[] generateI(int n, boolean other_sequence) {
if (n < 0) {
return null;
} else if (n == 0) {
return new double[0];
}
double[] arr = new double[n];
if (other_sequence) {
Arrays.setAll(arr, i -> ((i % 4) + (i / 4)));
} else {
Arrays.setAll(arr, i -> ((i % 4) - 1) * ((i + 1) % 2) * 0.5);
}
return arr;
}
public static void main(String[] args) {
System.out.println(Arrays.toString(generateI(8, false)));
System.out.println(Arrays.toString(generateI(16, true)));
}
}
Output:
[-0.5, 0.0, 0.5, 0.0, -0.5, 0.0, 0.5, 0.0]
[0.0, 1.0, 2.0, 3.0, 1.0, 2.0, 3.0, 4.0, 2.0, 3.0, 4.0, 5.0, 3.0, 4.0, 5.0, 6.0]
答案2
得分: 2
// 序列:-0.5, 0, 0.5, 0, -0.5, 0, 0.5...
public static double[] generateI(int n) {
if (n < 0) {
return null;
} else if (n == 0) {
return new double[0];
}
double[] arr = new double[n];
//for (int i = 0; i < n; i++) {
// arr[i] = ((i % 3) - 1) * 0.5;
//}
Arrays.arr.setAll(i -> ((i % 3) - 1) * 0.5);
// 更正,应为 ((i%4)-1)*((i+1)%2)*0.5
return arr;
}
有三个值,因此取模 3。然后考虑如何将 0、1、2 映射为 -0.5、0、0.5。
Arrays 类非常实用。
另一个序列由你自己努力完成。
英文:
// Sequence: -0.5, 0, 0.5, 0, -0.5, 0, 0.5...
public static double[] generateI(int n) {
if (n < 0) {
return null;
} else if (n == 0) {
return new double[0];
}
double[] arr = new double[n];
//for (int i = 0; i < n; i++) {
// arr[i] = ((i % 3) - 1) * 0.5;
//}
Arrays.arr.setAll(i -> ((i % 3) - 1) * 0.5);
// Correction, should be ((i%4)-1)*((i+1)%2)*0.5
return arr;
}
There are 3 values, hence modulo 3. Then think how 0, 1, 2 should be mapped onto -0.5, 0, 0.5.
Nice is the Arrays class.
The other sequence I leave to your own efforts.
答案3
得分: 0
有更简单的方式,我想
private static final double[] doubleArray = new double[] {-0.5, 0, 0.5, 0};
public static double[] generateI(int n) {
var resArray = new double[n];
for (int j = 0; j < n; j++) {
resArray[j] = doubleArray[j % 4];
}
return resArray;
}
英文:
There's much easier ways I think
private static final double[] doubleArray = new double[] {-0.5, 0, 0.5, 0};
public static double[] generateI(int n) {
var resArray = new double[n];
for (int j = 0; j<n; j++) {
resArray[j] = doubleArray[j % 4];
}
return resArray;
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论