需要帮助修改模数方程。

huangapple go评论62阅读模式
英文:

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 &lt; 0) {
		return null;
	} else if (n == 0) {
		return new double[0];
	}
	double[] arr = new double[n];
	for (int i = 0; i &lt; 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 标志来更改您要激活的两个模式中的哪一个。

对于您提到的第二个模式,您需要对 i4 取模(以返回 [0, 1, 2, 3]),然后将 i4 进行整数除法的结果相加,以使序列继续到下一个 4 个数字(例如 i = 66 / 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 -&gt; [1, 2, 3, 4])

class Main {
    public static double[] generateI(int n, boolean other_sequence) {
        if (n &lt; 0) {
            return null;
        } else if (n == 0) {
            return new double[0];
        }
        double[] arr = new double[n];

        if (other_sequence) {
            Arrays.setAll(arr, i -&gt; ((i % 4) + (i / 4)));
        } else {
            Arrays.setAll(arr, i -&gt; ((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 &lt; 0) {
        return null;
    } else if (n == 0) {
        return new double[0];
    }
    double[] arr = new double[n];
    //for (int i = 0; i &lt; n; i++) {
    //    arr[i] = ((i % 3) - 1) * 0.5;
    //}
    Arrays.arr.setAll(i -&gt; ((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&lt;n; j++) {
            resArray[j] = doubleArray[j % 4];
        }
        
        return resArray;
    }

huangapple
  • 本文由 发表于 2020年9月11日 16:00:43
  • 转载请务必保留本文链接:https://go.coder-hub.com/63843071.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定