(编辑:在更多人投反对票之前,我确实事先查看了 Javadoc,但由于我是初学者,所以我不确定要查看文档的何处。请参阅我对 Jim G 的回复,该回复发布在下面。我了解这一点这个问题可能被认为太基础了。但我认为它对我这种情况的其他初学者有一定的价值。所以请在投反对票之前从初学者的角度考虑完整的情况。)
我想将 BigInteger 除以常规整数(即 int),但我不知道该怎么做。我在 Google 和 Stack Exchange 上进行了快速搜索,但没有找到任何答案。
那么,如何将 BigInteger 除以 int 呢?当我们这样做时,如何将 BigInts 添加/减去 ints,将 BigInts 与 ints 进行比较,等等?
package fibonacci;
import java.math.BigInteger;
import java.math.*;
public class fibo {
public static void main(String[] args) {
//number of elements to generate in a series
BigInteger sum;
BigInteger[] series= new BigInteger[4000000];
//long[] series = new long[4000000];
//create first 2 series elements
series[0] = BigInteger.valueOf(1);
series[1] = BigInteger.valueOf(2);
//create the Fibonacci series and store it in an array
for(int i=2; i < series.length; i++){
series[i] = series[i-1].add(series[i-2]);
if (series[i].mod(BigInteger.valueOf(2)) == BigInteger.valueOf(0)){
sum = sum.add(series[i]); //error here
}
}
System.out.println("Fibonacci Series sum " …Run Code Online (Sandbox Code Playgroud) 所以我编写了可以找到N'th Fibonacci数的java代码.我的问题是,我必须输出答案%1000000007.我以为我可以通过写作来完成
System.out.println(answer.mod(1000000007));
Run Code Online (Sandbox Code Playgroud)
但它不允许我这样做,我得到一个错误"BigInteger类型中的方法mod(BigInteger)不适用于参数(int)"
有人可以帮助我,这是我的代码.
import java.io.IOException;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.util.Scanner;
public class dmoj
{
public static void main(String[] args) throws IOException{
Scanner scan = new Scanner(System.in);
BigDecimal d, v, t, a;
BigInteger b;
int index;
int n = scan.nextInt();
double phi1 = (1 + Math.sqrt(5)) / 2;
double phi2 = (1 - Math.sqrt(5)) / 2;
double sqrt = Math.sqrt(5);
d = new BigDecimal(phi1);
v = new BigDecimal(phi2);
a = new BigDecimal(sqrt);
d = d.pow(n);
v = v.pow(n); …Run Code Online (Sandbox Code Playgroud) 我目前正在完成一个项目,并遇到了一个问题。我正在编写的程序需要大数字,大于 int 可以容纳的数字。所以,我使用了 Math.BigInteger。将许多 BigInteger 添加在一起后,出现 OutofMemory 错误。我假设这意味着数据类型不够大。是否有更大的数据类型,如果没有,我该如何解决这个问题?
我需要从字符串转换此值:4248035245857302861304475122262852382232831183377907185400973044372526725256648804647567360并将其保存在int中.长,Int64数据类型不起作用.我不能使用BigInteger.双人不能分.一些建议?
string valorLido;
while ((valorLido = Console.ReadLine()) != null)
{
int leapYear = 0;
int huluculuFestival = 0;
int bulukuluFestival = 0;
long ano = long.Parse(valorLido);
if ((ano % 4 == 0) && (ano % 100 != 0 || ano % 400 == 0))
leapYear = 1;
if (ano % 15 == 0)
huluculuFestival = 1;
if (leapYear == 1 && ano % 55 == 0)
bulukuluFestival = 1;
if(leapYear == 1)
Console.WriteLine("This is leap year.");
if(huluculuFestival == …Run Code Online (Sandbox Code Playgroud) 我正在使用java,我正在尝试使用不同基数(2,8和10)的数字.
所以,我问这个问题是将字符串格式的二进制数改成基数为10的数字
例如110000001010100000000000000000111111/base 2
我尝试了Integer.parseInt(str,radix),但是我遇到了错误.exepetion""线程中的异常"main"java.lang.NumberFormatException:对于java.lang上的java.lang.NumberFormatException.forInputString(Unknown Source)java.lang.Integer.parseInt(Unknown Source)中的输入字符串:"110000001010100000000000000111111" .Integer.parseInt(未知来源)