java大數類操作以及應用(UVA)

來源:互聯網
上載者:User

標籤:acm   uva   java   大數   biginteger   


首先,先看看java大數的基本操作

Ⅰ基本函數:1.valueOf(parament); 將參數轉換為制定的類型
  比如 int a=3;
  BigInteger b=BigInteger.valueOf(a);
  則b=3;
  String s=”12345”;
  BigInteger c=BigInteger.valueOf(s);
  則c=12345;


2.add(); 大整數相加
BigInteger a=new BigInteger(“23”);
BigInteger b=new BigInteger(“34”);
a.      add(b);


3.subtract(); 相減
4.multiply(); 相乘
5.divide();    相除取整
6.remainder(); 取餘
7.pow();   a.pow(b)=a^b
8.gcd();   最大公約數
9.abs(); 絕對值
10.negate(); 取反數
11.mod(); a.mod(b)=a%b=a.remainder(b);
12.max(); min();
13.punlic int compareTo();//比較,>返回1,==返回0,<返回-1;
14.boolean equals(); 是否相等
15.BigInteger建構函式:
一般用到以下兩種:
BigInteger(String val);
將指定字串轉換為十進位表示形式;
BigInteger(String val,int radix);
將指定基數的 BigInteger 的字串表示形式轉換為 BigInteger
Ⅱ.基本常量:A=BigInteger.ONE    1
B=BigInteger.TEN    10
C=BigInteger.ZERO   0
Ⅲ.基本操作1.   讀入:
用Scanner類定義對象進行控制台讀入,Scanner類在java.util.*包中
Scanner cin=new Scanner(System.in);// 讀入
while(cin.hasNext())   //等同於!=EOF
{
 int n;
 BigInteger m;
 n=cin.nextInt(); //讀入一個int;
 m=cin.BigInteger();//讀入一個BigInteger;
 System.out.print(m.toString());
}









接下來是UVA上的題目,練練手:



AC代碼:

import java.math.BigInteger;import java.text.*;import java.util.Scanner;public class Main {public static void main(String args[]) {Scanner cin = new Scanner(System.in);while(cin.hasNext()) {BigInteger n = cin.nextBigInteger();BigInteger m = cin.nextBigInteger();m = m.multiply(n);System.out.println(m);}}}










AC代碼:

import java.math.*;import java.util.Scanner;public class Main {public static void main(String[] args) {// TODO Auto-generated method stubScanner cin = new Scanner(System.in);while(cin.hasNext()) {BigDecimal r = cin.nextBigDecimal();int n = cin.nextInt();r = r.pow(n);String str = r.stripTrailingZeros().toPlainString();if (str.startsWith("0."))str = str.substring(1);System.out.println(str);}}}






說明:
1、stripTrailingZeros() ,傳回型別為BigDecimal的小於此數的但除去尾部的0的數值。
2、toPlainString(),返回BigDecimal類型的String類型字串。
3、startsWith(),確定此執行個體的開頭是否與指定的字串匹配。
4、substring(),返回一個新的字串,它是此字串的一個子字串。該子字串始於指定索引處的字元,一直到此字串末尾。








AC代碼:

import java.math.*;import java.util.Scanner;public class Main {public static void main(String args[]) {Scanner cin = new Scanner(System.in);BigInteger t = BigInteger.ZERO;while(true) {BigInteger r = cin.nextBigInteger();if(r.equals(BigInteger.ZERO)) break;t = t.add(r);}System.out.println(t);}}




========2015/3/28  13:27












java大數類操作以及應用(UVA)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.