gpt4 book ai didi

java - Math.pow() 使用不同的输入返回相同的结果

转载 作者:行者123 更新时间:2023-12-04 20:48:48 26 4
gpt4 key购买 nike

我有两个空数组和一些变量:

static int p = 41;
static int q = 67;
static int d = 83;
static int n = p * q;

static int[] encryptedBlocks = new int[charArray.length];
static int[] decryptedArray = new int[charArray.length];

其中第一个填充了以下内容(我已经检查过了,确实如此):

1573
1978
385
1092
1022
857
856
1387
225
606
2293
1339
1630

我试图用 for 循环中方程的结果填充第二个数组:

for (int i = 0; i < encryptedBlocks.length; i++) {
int M = (int)Math.pow(encryptedBlocks[i], d) % n;
decryptedArray[i] = M;
}

问题是每次迭代我都得到相同的 M 结果。我完全不知道发生了什么:

2662
2662
2662
2662
2662
2662
2662
2662
2662
2662
2662
2662
2662

以防万一,我仔细检查过 encryptedBlocks[i] 确实是每次迭代的下一个值,确实如此。我是否缺少与使用 intMath.pow() 相关的内容?

encryptedBlocks[i]dn 的前两个迭代值:

1573 83 2747 
1978 83 2747

最佳答案

如前所述here :

a cast takes precedence over a division operation

因此,正如我在评论中提到的,您必须替换它:

int M = (int)Math.pow(encryptedBlocks[i], d) % n;

有了这个:

int M = (int)(Math.pow(encryptedBlocks[i], d) % n);

关于java - Math.pow() 使用不同的输入返回相同的结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43036147/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com