java大数

来源:互联网 发布:eclipse写java程序 编辑:程序博客网 时间:2024/06/08 04:58

Avaricious Maryanna UVA-12009

After Maryanna found the treasure buried by 27 pilots in a secret cave, she wanted to leave there
immediately. Unfortunately, nding the door closed because of the overweight treasure she carried, she
had to nd out the password of the lock. She remembered someone had told her the password is an
N
-digit natural decimal integer (without extra leading zeroes), and the
N
least signi cant digits of any
positive integral power of that integer is same as itself. She, being a smart girl, came up with all the
possible answers within 1 minute. After a few times of tries, she escaped from that cave successfully.
To show your intelligence you may solve the same task with your computer within only 10 seconds!

Input

The rst line contains
T(T<=1000), the number of test cases.
T lines follow, each contains a single positive integer N(N<=500).

Output

For each test case, output a single line, which should begin with the case number counting from 1,
followed by all the possible passwords sorted in increasing order. If no such passwords exist, output’Impossible’ instead. See the sample for more format details.

Sample Input

2
2
1

Sample Output

Case #1: 25 76
Case #2: 0 1 5 6


题意:
输入一个数n
输出一个n位数
这个n位数的任意指数次幂的最低n位数都是n

import java.math.BigInteger;import java.util.*;public class Main{    public static void main(String[] args) {        BigInteger[][] biao=new BigInteger[600][5];        biao[2][0]=new BigInteger("25");        biao[2][1]=new BigInteger("76");        BigInteger shi=BigInteger.ONE;        BigInteger ping;        for(int i=3;i<=500;i++)        {            shi=BigInteger.ONE;            for(int j=0;j<i;j++)            {                shi=shi.multiply(BigInteger.TEN);            }            ping=biao[i-1][0].multiply(biao[i-1][0]);            biao[i][0]=ping.mod(shi);            ping=biao[i-1][1].multiply(biao[i-1][1]);            ping=ping.mod(shi);            ping=ping.multiply(BigInteger.TEN);            ping=ping.divide(shi);            ping=BigInteger.TEN.subtract(ping);            ping=ping.mod(BigInteger.TEN);            ping=ping.multiply(shi);            ping=ping.divide(BigInteger.TEN);            biao[i][1]=biao[i-1][1].add(ping);        }        BigInteger fans=biao[500][0];        BigInteger sans=biao[500][1];        Scanner sin=new Scanner (System.in);        int T=sin.nextInt();        for(int i=1;i<=T;i++)        {            int n=sin.nextInt();            if(n==1)            {                System.out.println("Case #"+i+": 0 1 5 6");            }            else            {                shi=BigInteger.ONE;                int flag=0;                for(int j=1;j<=n;j++)                {                    shi=shi.multiply(BigInteger.TEN);                }                BigInteger sec=fans.mod(shi);                BigInteger fir=sans.mod(shi);                System.out.print("Case #"+i+":");                if(fir.compareTo(sec)>0)                {                    shi=fir;                    fir=sec;                    sec=shi;                }                shi=BigInteger.ONE;                for(int j=1;j<n;j++)                {                    shi=shi.multiply(BigInteger.TEN);                }                if(fir.compareTo(shi)>=0)                {                    System.out.print(" "+fir);                    flag=1;                }                if(sec.compareTo(shi)>=0)                {                    System.out.print(" "+sec);                    flag=1;                }                if(flag==0)                {                    System.out.print(" Impossible");                }                System.out.println();            }        }    }}

A + B Problem II

Problem Description

I have a very simple problem for you. Given two integers A and B, your job is to calculate the Sum of A + B.

Input

The first line of the input contains an integer T(1<=T<=20) which means the number of test cases. Then T lines follow, each line consists of two positive integers, A and B. Notice that the integers are very large, that means you should not process them by using 32-bit integer. You may assume the length of each integer will not exceed 1000.

Output

For each test case, you should output two lines. The first line is “Case #:”, # means the number of the test case. The second line is the an equation “A + B = Sum”, Sum means the result of A + B. Note there are some spaces int the equation. Output a blank line between two test cases.

Sample Input

2
1 2
112233445566778899 998877665544332211

Sample Output

Case 1:
1 + 2 = 3

Case 2:
112233445566778899 + 998877665544332211 = 1111111111111111110

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

方法举例:

import java.math.BigInteger;public class Main{    public static void main(String[] args) {        BigInteger a;        BigInteger b=new BigInteger("456");        a=new BigInteger("123456789");//a=123456789        a=BigInteger.TEN;//a=10        a=BigInteger.ONE;//a=1        a=BigInteger.ZERO;//a=0        a=a.add(BigInteger.ONE);//a=a+1        a=a.subtract(b);//a-=b        a=a.abs();//  |a|        a=a.multiply(b);//a*=b        BigInteger[] m;//an array        m=a.divideAndRemainder(BigInteger.TEN);        //Returns an array of two BigIntegers containing (this / val) followed by (this % val).        BigInteger c=b.divide(a);//c=b/a        c=b.mod(a);//c=b%a        if(a.compareTo(b)<0)//a<b            a=a.shiftLeft(2);//a=a<<2        //shiftRight  >>        a.modPow(c, b);//快速幂取模  a^c%b 都要biginteger类型        a.pow(9);//a^9  int 类型        c=a.gcd(b);//c=gcd(a,b)        c.xor(a);//c^a        c.and(a);//c&a        c.andNot(a)//c&~a        c.not();//~c        a=a.min(b);//min(a,b)        int C=c.intValue()//转为int               long CC=c.longValue();//转long        BigDecimal a=new BigDecimal("3.1415926535");        //new BigDecimal()String long int double BigInteger char[]        BigDecimal b=new BigDecimal(5846.57456);        a=b.setScale(2, BigDecimal.ROUND_HALF_UP);//设置舍入模式    }}

ROUND_CEILING
Rounding mode to round towards positive infinity.
向正无穷方向舍入

ROUND_DOWN
Rounding mode to round towards zero.
向零方向舍入

ROUND_FLOOR
Rounding mode to round towards negative infinity.
向负无穷方向舍入

ROUND_HALF_DOWN
Rounding mode to round towards “nearest neighbor” unless both neighbors are equidistant, in which case round down.
向(距离)最近的一边舍入,除非两边(的距离)是相等,如果是这样,向下舍入, 例如1.55 保留一位小数结果为1.5

ROUND_HALF_EVEN
Rounding mode to round towards the “nearest neighbor” unless both neighbors are equidistant, in which case, round towards the even neighbor.
向(距离)最近的一边舍入,除非两边(的距离)是相等,如果是这样,如果保留位数是奇数,使用ROUND_HALF_UP ,如果是偶数,使用ROUND_HALF_DOWN

ROUND_HALF_UP
Rounding mode to round towards “nearest neighbor” unless both neighbors are equidistant, in which case round up.
向(距离)最近的一边舍入,除非两边(的距离)是相等,如果是这样,向上舍入, 1.55保留一位小数结果为1.6

ROUND_UNNECESSARY
Rounding mode to assert that the requested operation has an exact result, hence no rounding is necessary.
计算结果是精确的,不需要舍入模式

ROUND_UP
Rounding mode to round away from zero.
向远离0的方向舍入

原创粉丝点击