avatar
fireworks99
keep hungry keep foolish

JAVA 大数

条件判断: JAVA中while(n-- > 0)异于c++的while(n--)

Prepare to Input

Scanner in = new Scanner(System.in);//in可换为cin或scan
//......
in.close();//in可换cin或scan

Input 、Initialize and Output

import java.math.BigDecimal;
import java.math.BigInteger;
import java.util.Scanner;

public class Main {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        Scanner in = new Scanner(System.in);
        //以下输入后面的括号是用于进制转换的,输入某进制数将其转十进制
        //JAVA强大处之一
        int a = in.nextInt();
        double b = in.nextDouble();
        BigInteger c = in.nextBigInteger();
        BigDecimal d = in.nextBigDecimal();
        in.nextLine();//getchar();
        String k = in.next();        //cin >> k;
        String e = in.nextLine();    //getline(cin, e);
        int f = 6;
        double g = 3.1415926;
        BigInteger h = new BigInteger("666");//不带引号毁精度
        BigDecimal i = new BigDecimal("3.1415926");
        String j = "LX";
        System.out.println(a);
        System.out.println(b);
        System.out.println(c);
        System.out.println(d);
        System.out.println(e);
        System.out.println(f);
        System.out.println(g);
        System.out.println(h);
        System.out.println(i);
        System.out.println(j);
        System.out.printf("%d\n",a);
        in.close();
    }

}

运算

BigInteger a = BigInteger.valueOf(200);//无引号
BigInteger b = BigInteger.TEN;
a = a.add(b);
a = a.subtract(b);
a = a.multiply(b);
a = a.divide(b);
a = a.mod(b);

HDU 1002(大数加法)

http://acm.hdu.edu.cn/showproblem.php?pid=1002

Code

import java.util.*;
import java.io.BufferedInputStream;
import java.math.BigInteger;
public class Main {
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        Scanner cin = new Scanner(new BufferedInputStream(System.in));
        int n = cin.nextInt();
        int i = 1;
        while(n-- > 0) 
        {
            BigInteger a = cin.nextBigInteger();
            BigInteger b = cin.nextBigInteger();
            System.out.println("Case " + i + ":");
            System.out.println(a + " + " + b + " = " + a.add(b));
            if(n > 0)
                System.out.println();
            i++;
        }
        cin.close();
    }
}

HDU 1042 (N!)

http://acm.hdu.edu.cn/showproblem.php?pid=1042

Code

import java.io.BufferedInputStream;
import java.math.BigInteger;
import java.util.Scanner;

public class Main {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        Scanner in = new Scanner(new BufferedInputStream(System.in));
        while(in.hasNextInt()) {
            int n = in.nextInt();
            BigInteger ans = new BigInteger("1");
            BigInteger num = new BigInteger("1");
            for(int i = 1; i <= n; ++i) {
                ans = ans.multiply(num);
                num = num.add(new BigInteger("1"));
            }
            System.out.println(ans);
        }
        in.close();
    }

}

HDU 1250(Fibonacci)

http://acm.hdu.edu.cn/showproblem.php?pid=1250

Code

import java.io.BufferedInputStream;
import java.math.BigInteger;
import java.util.Scanner;

public class Main {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        Scanner in = new Scanner(new BufferedInputStream(System.in));
        BigInteger [] f = new BigInteger[10005];
        f[1] = f[2] = f[3] = f[4] = new BigInteger("1");
        for(int i = 5; i < 10005; ++i)
            f[i] = f[i - 1].add(f[i - 2]).add(f[i - 3]).add(f[i - 4]);
        while(in.hasNext()) {
            int n = in.nextInt();
            System.out.println(f[n]);
        }
        in.close();
    }

}

POJ 3199(N ^ M)

http://poj.org/problem?id=3199

Code

import java.io.BufferedInputStream;
import java.math.BigInteger;
import java.util.Scanner;



public class Main {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        Scanner in = new Scanner(new BufferedInputStream(System.in));
        BigInteger n, m, ans;
        while(in.hasNext()) {
            n = in.nextBigInteger();
            m = in.nextBigInteger();
            if(n.compareTo(BigInteger.ZERO) == 0 && m.compareTo(BigInteger.ZERO) == 0)
                break;
            ans = n.pow(m.intValue());
            System.out.println(ans);
        }
        in.close();
    }

}

关于compareTo

compareTo() 方法用于将 Number 对象与方法的参数进行比较。可用于比较 Byte, Long, Integer等。

该方法用于两个相同数据类型的比较,两个不同类型的数据不能用此方法来比较。

  • 如果指定的数与参数相等返回0。
  • 如果指定的数小于参数返回 -1。
  • 如果指定的数大于参数返回 1。

还有记得ans = n.pow(m.intValue());

SDNU 1488

输入两个八进制数a、b,也以八进制形式输出(a - b)的值。

Code

import java.math.BigInteger;
import java.util.Scanner;


public class Main {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        Scanner in = new Scanner(System.in);
        while(in.hasNextInt()) {
            int t = in.nextInt();
            while(t-- > 0) {
                //将数字以8进制读入并转为十进制
                BigInteger c = in.nextBigInteger(8);
                BigInteger d = in.nextBigInteger(8);
                BigInteger ans;
                if(c.subtract(d) == BigInteger.valueOf(1) || c == d);
                    {
                     ans = c.subtract(d);//十进制减法
                     String a = ans.toString(8);//十进制数ans转为8进制字符串
                    System.out.println(a);
                    }
                if(c.subtract(d) == BigInteger.valueOf(-1))
                {
                    ans = d.subtract(c);
                    String a = ans.toString(8);
                    System.out.println("-" + a);
                }
            }
        }
        in.close();
    }

}

进制转换

String a = ans.toString(num); //将十进制数ans转为num进制字符串

已知数字的进制转换

十进制转其他进制

        int i = 36;
        String a = Integer.toBinaryString(i);//二
        String b = Integer.toHexString(i);//十六
        String c = Integer.toOctalString(i);//八
        String d = Integer.toString(i,35);// <= 35进制
Site by Baole Zhao | Powered by Hexo | theme PreciousJoy