Java uses BigInteger and while
•
Java
I have a question
import java.math.BigInteger;
import java.io.*;
import java.util.*;
import java.lang.*;
public class medici {
public static void main(String[] arg) {
{
BigInteger zac = new BigInteger("3");
zac = zac.pow(399);
BigInteger kon = new BigInteger("3");
kon = kon.pow(400);
BigInteger nul = new BigInteger("0");
BigInteger jed = new BigInteger("1");
BigInteger detel = new BigInteger("3");
for (BigInteger a = zac; a.compareTo( kon ) <= 0; a = a.add(jed)) {
cis = a ; // THIS A PROBLEM
String retez = "";
while ( cis > 0 ); // THIS IS A PROBLEM
retez = ( cis.mod(detel) ) + retez;
cis = cis.divide(detel);
System.out.println(retez);
}
}
}
}
I tried the formula BigInteger CIS = new BigInteger ("a"); This CIS = a;
While (CIS. CompareTo (nul) > 0); For this purpose (CIS > 0);
But it doesn't work. I don't know why
When I use this formula, it's the same, but when I use the same for big integer, I only use integers, which doesn't work
import java.io.*;
import java.util.*;
import java.lang.*;
public class netik {
public static void main(String[] arg) {
{
int a ;
int cis;
int detel = 3;
for ( a = 567880; a <= 567890; a++ ){
cis = a;
String retez = "";
while (cis > 0) {
retez = (cis % detel) + retez;
cis /= detel;
}
System.out.println(retez);
}
}
}
}
Solution
To declare and store CIS in CIS, see the following:
BigInteger cis = new BigInteger(""+a);
Assume that this code is the main cause of the problem and that CIS is BigInteger:
while (cis > 0) {
retez = (cis % detel) + retez;
cis /= detel;
}
This should be: (this assumes that everything is BigInteger
while (cis.compareTo(new BigInteger("0")) > 0) {
retez = (cis.mod(detel)).add(retez);
cis = cis.divide(detel);
}
The following code runs for me:
public static void main(String[] arg) {
BigInteger zac = new BigInteger("3");
zac = zac.pow(399);
BigInteger kon = new BigInteger("3");
kon = kon.pow(400);
BigInteger nul = new BigInteger("0");
BigInteger jed = new BigInteger("1");
BigInteger detel = new BigInteger("3");
for (BigInteger a = zac; a.compareTo(kon) <= 0; a = a.add(jed)) {
BigInteger cis = a; // THIS A PROBLEM
String retez = "";
while (cis.compareTo(new BigInteger("0")) >= 0) {
retez = (cis.mod(detel)) + retez;
cis = cis.divide(detel);
System.out.println(retez);
}
}
}
It will not produce the best results But it works
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
二维码
