compare

 avatar
unknown
java
4 years ago
1.2 kB
6
Indexable
import java.math.BigInteger;
public class HelloWorld{
// so1 < so2 -1
// so1 > so2 1
// so1 == so2 0
    static public int compare(BigInteger so1, BigInteger so2)
    {	
		String str1 = so1.toString();
		String str2 = so2.toString();
		if (str1.equals(str2)) return 0;
        // Calculate lengths of both string
        int n1 = str1.length(), n2 = str2.length();
        if (n1 < n2)
            return -1;
        if (n2 < n1)
            return 1;
 
        for (int i = 0; i < n1; i++)
            if (str1.charAt(i) < str2.charAt(i))
                return -1;
            else if (str1.charAt(i) > str2.charAt(i))
                return 1;
 
        return 1;
    }
     public static void main(String []args){
         BigInteger b1, b2;
        System.out.println("Hello World");
         b1 = new BigInteger("5399343357487366468553652211764549760094816697168792531537008467452650182734572361721480021422161238021228385014727182859448754964483833696635176808626");
        b2 = new BigInteger("5399343357487366468553652211764549760094816697168792531537008467452650182734572361721480021422161238021228385014727182859448754964483833696635176808627");
    System.out.println(compare(b2,b1));
     }
}
Editor is loading...