JAVA大數類練手

來源:互聯網
上載者:User

今天突然看到了OJ上的大數類題目,由於學習了一點大數類的知識。果斷水了6道題。。。。。。都是非常基礎的。就當的練手的吧。

學到的只是一些大數類的基本操作。以後多做點這樣的題,爭取熟練運用水大數題。。。

大數階乘

題目連結:http://acm.nyist.net/JudgeOnline/problem.php?pid=28

代碼如下:

import java.io.*;import java.math.BigInteger;import java.util.*;public class Main{public static void main(String args[]){Scanner cin = new Scanner(System.in);int n = cin.nextInt();BigInteger ans = BigInteger.ONE;for(int i = 1; i <= n; ++i)ans = ans.multiply(BigInteger.valueOf(i));System.out.println(ans);}}

棋盤覆蓋

題目連結:http://acm.nyist.net/JudgeOnline/problem.php?pid=45

代碼如下:

import java.math.BigInteger;import java.util.*;import java.io.*;public class Main{public static void main(String args[]){Scanner in = new Scanner(System.in);int test = in.nextInt();while(test-- > 0){int n;n = in.nextInt();BigInteger a = new BigInteger("4");for(int i = 1; i < n; ++i)a = a.multiply(BigInteger.valueOf(4));System.out.println(a.subtract(BigInteger.valueOf(1)).divide(BigInteger.valueOf(3)));}}}

比較大小

題目連結:http://acm.nyist.net/JudgeOnline/problem.php?pid=73

代碼如下:

import java.io.*;import java.math.BigInteger;import java.util.*;public class Main{public static void main(String args[]){Scanner cin = new Scanner(System.in);while(cin.hasNext()){BigInteger a = cin.nextBigInteger();BigInteger b = cin.nextBigInteger();if(a.equals(BigInteger.ZERO) && b.equals(BigInteger.ZERO))break;int flag = a.compareTo(b);if(flag == -1)System.out.println("a<b");else if(flag == 0)System.out.println("a==b");elseSystem.out.println("a>b");}}}

大數加法

題目連結:http://acm.nyist.net/JudgeOnline/problem.php?pid=103

代碼如下:

import java.math.BigInteger;import java.util.*;import java.io.*;public class Main{public static void main(String args[]){Scanner in = new Scanner(System.in);int n = in.nextInt();for(int i = 1; i <= n; ++i){BigInteger a = in.nextBigInteger();BigInteger b = in.nextBigInteger();BigInteger ans = a.add(b);System.out.println("Case " + i + ":");System.out.println(a + " + " + b + " = " +ans);}}}

遞推求值

題目連結:http://acm.nyist.net/JudgeOnline/problem.php?pid=114

代碼如下:

import java.io.*;import java.math.BigInteger;import java.util.*;public class Main{public static void main(String args[]){Scanner cin = new Scanner(System.in);BigInteger a[] = new BigInteger[100];while(cin.hasNext()){for(int i = 0; i <= 2; ++i)a[i] = cin.nextBigInteger();for(int i = 3; i <= 99; ++i)a[i] = a[i - 1].add(a[i - 2]).add(a[i - 3]);System.out.println(a[99]);}}}

高精度冪

題目連結:http://acm.nyist.net/JudgeOnline/problem.php?pid=155

代碼如下:

import java.io.*;import java.math.BigDecimal;import java.util.*;public class Main{public static void main(String args[]){Scanner cin = new Scanner(System.in);while(cin.hasNext()){BigDecimal ans = cin.nextBigDecimal();int n = cin.nextInt();String res = ans.pow(n).stripTrailingZeros().toPlainString(); //整數去掉小數點和後面的0if(res.startsWith("0")) //去掉前置0{res = res.substring(1);}System.out.println(res);}}}
相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.