Java中的模幂问题 import java.util.Scanner; 类别代码abbey145 { 公共静态void main(字符串[]Args) { 扫描仪输入=新扫描仪(System.in); 双A=0; 双B=0; 双M=0; System.out.println(“\n\n有多少套?”); int s=input.nextInt(); 双倍X[]=新的双倍; 对于(int i=0;i
而言,值很可能太大,无法放入Java中的模幂问题 import java.util.Scanner; 类别代码abbey145 { 公共静态void main(字符串[]Args) { 扫描仪输入=新扫描仪(System.in); 双A=0; 双B=0; 双M=0; System.out.println(“\n\n有多少套?”); int s=input.nextInt(); 双倍X[]=新的双倍; 对于(int i=0;i,java,modular,exponentiation,Java,Modular,Exponentiation,而言,值很可能太大,无法放入双精度,因此它们会溢出。最好使用biginger对象实现 具有可用于计算的pow和mod函数。您的代码绝对正确:检查 可能您应该使用biginger:来处理大数字,不建议使用double 下面是一个工作示例:选中此 代码 import java.util.Scanner; class codeabbey145 { public static void main(String[] Args) { Scanner input = n
双精度
,因此它们会溢出。最好使用biginger
对象实现
具有可用于计算的
pow
和mod
函数。您的代码绝对正确:检查
可能您应该使用biginger:
来处理大数字,不建议使用double
下面是一个工作示例:选中此
代码
import java.util.Scanner;
class codeabbey145
{
public static void main(String[] Args)
{
Scanner input = new Scanner(System.in);
double A = 0;
double B = 0;
double M = 0;
System.out.println("\n\nHow many sets?");
int s = input.nextInt();
double X[] = new double[s];
for(int i = 0; i<s; i++)
{
System.out.println("A: ");
A = input.nextDouble();
System.out.println("B: ");
B = input.nextDouble();
System.out.println("M: ");
M = input.nextDouble();
X[i] = (Math.pow(A, B)) % M; //(A^B)%M
}
for(int j = 0; j<s; j++)
{
System.out.print(Math.round(X[j]) + " ");
}
}
}
publicstaticvoidmain(字符串[]Args){
扫描仪输入=新扫描仪(System.in);
System.out.println(“\n\n有多少套?”);
int s=input.nextInt();
BigInteger[]X=新的BigInteger[s];
对于(int i=0;i
我已经试过了。
我的解决方案被接受了
代码:
public static void main(String[] Args) {
Scanner input = new Scanner(System.in);
System.out.println("\n\nHow many sets?");
int s = input.nextInt();
BigInteger[] X = new BigInteger[s];
for (int i = 0; i < s; i++) {
System.out.println("A: ");
BigInteger A = input.nextBigInteger();
System.out.println("B: ");
BigInteger B = input.nextBigInteger();
System.out.println("M: ");
BigInteger M = input.nextBigInteger();
X[i] = A.modPow(B, M); //(A^B)%M
}
for (int i = 0; i < X.length; i++) {
System.out.println(X[i]);
}
}
import java.util.*;
导入java.lang.*;
导入java.io.*;
导入java.math.biginger;
/*只有当类是公共的时,类的名称才必须是“Main”*/
表意文字
{
公共静态void main(字符串[]Args){
扫描仪输入=新扫描仪(System.in);
int s=input.nextInt();
BigInteger[]X=新的BigInteger[s];
对于(int i=0;i
很可能值太大,无法放入双精度。您最好的选择可能是使用biginger
s实现。为什么它不起作用?您为a
和B
提供了什么值?此外,biginger
有一个modPow
方法。是的,我知道,但CodeAbbey分配说明:虽然有些语言有内置函数来进行这种计算,当然最好找到另一种方法!然后尝试使用long,为什么要使用double,是否需要计算((2.3)^(1.234)%2.456)?
import java.util.*;
import java.lang.*;
import java.io.*;
import java.math.BigInteger;
/* Name of the class has to be "Main" only if the class is public. */
class Ideone
{
public static void main(String[] Args) {
Scanner input = new Scanner(System.in);
int s = input.nextInt();
BigInteger[] X = new BigInteger[s];
for (int i = 0; i < s; i++) {
BigInteger A = input.nextBigInteger();
BigInteger B = input.nextBigInteger();
BigInteger M = input.nextBigInteger();
X[i] = A.modPow(B, M);
}
for (int i = 0; i < X.length; i++) {
System.out.println(X[i]+" ");
}
}
}