Java大數練習第一彈

來源:互聯網
上載者:User

標籤:java java大數 acm 卡蘭特數

今天要多刷java大數,爭取以後輕鬆水掉大數題~


hdu 1002 大數相加 連結:http://acm.hdu.edu.cn/showproblem.php?pid=1002

【代碼】

import java.util.*;import java.math.BigInteger;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,b,c;            a=in.nextBigInteger();            b=in.nextBigInteger();            c=a.add(b);            if(i!=1)                System.out.println();            System.out.println("Case "+i+":");            System.out.println(a+" + "+b+" = "+c);        }    }}

hdu 1042 大數的階乘 連結:http://acm.hdu.edu.cn/showproblem.php?pid=1042

【代碼】


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

hdu 1133 卡蘭特數的運用 題目連結:http://acm.hdu.edu.cn/showproblem.php?pid=1133


【代碼】

mport java.util.*;import java.math.BigInteger;public class Main{    public static void main(String[] args){            int a,b;            Scanner in=new Scanner(System.in);            int cnt=0;            while(in.hasNext()){                cnt++;                a=in.nextInt();                b=in.nextInt();                BigInteger ans=BigInteger.ONE;                if(a==0&&b==0)break;                if(a<b) ans=BigInteger.ZERO;                else {                    for(int i=1;i<=a+b;i++){                        ans=ans.multiply(BigInteger.valueOf(i));                    }                    int mpl=(a-b+1);                    int dvd=(a+1);                    ans=ans.multiply(BigInteger.valueOf(mpl));                    ans=ans.divide(BigInteger.valueOf(dvd));                }                System.out.println("Test #"+cnt+":");                System.out.println(ans);            }        }}



著作權聲明:本文為博主原創文章,未經博主允許不得轉載。

Java大數練習第一彈

聯繫我們

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