java大法好

2 篇文章 0 订阅

将一个 a.bed的数转换成正常形式

import java.math.*;
import java.util.*;
 
 
public class Main {
       public static void main(String[] args)
       {
          Scanner cin = new Scanner (System.in); 
          BigDecimal a = cin.nextBigDecimal();//可以读入指数呦
          a = a.stripTrailingZeros();//清除末尾的0
          System.out.println(a.toPlainString());
         //toPlainString    返回不带指数字段的此 BigDecimal 的字符串表示形式
       }
}

大数阶乘

题目链接:http://acm.nyist.net/JudgeOnline/problem.php?pid=28

代码如下:

[cpp]  view plain  copy
  1. import java.io.*;    
  2. import java.math.BigInteger;    
  3. import java.util.*;    
  4.     
  5. public class Main    
  6. {    
  7.     public static void main(String args[])    
  8.     {    
  9.         Scanner cin = new Scanner(System.in);       
  10.         int n = cin.nextInt();    
  11.         BigInteger ans = BigInteger.ONE;    
  12.         for(int i = 1; i <= n; ++i)    
  13.             ans = ans.multiply(BigInteger.valueOf(i));    
  14.         System.out.println(ans);    
  15.     }    
  16. }    

棋盘覆盖

题目链接:http://acm.nyist.net/JudgeOnline/problem.php?pid=45

代码如下:

[java]  view plain  copy
  1. import java.math.BigInteger;    
  2. import java.util.*;    
  3. import java.io.*;    
  4.     
  5. public class Main    
  6. {    
  7.     public static void main(String args[])    
  8.     {    
  9.         Scanner in = new Scanner(System.in);    
  10.         int test = in.nextInt();    
  11.         while(test-- > 0)    
  12.         {    
  13.             int n;    
  14.             n = in.nextInt();    
  15.             BigInteger a = new BigInteger("4");    
  16.             for(int i = 1; i < n; ++i)    
  17.                 a = a.multiply(BigInteger.valueOf(4));    
  18.             System.out.println(a.subtract(BigInteger.valueOf(1)).divide(BigInteger.valueOf(3)));    
  19.         }    
  20.     }    
  21. }    

比较大小

题目链接:http://acm.nyist.net/JudgeOnline/problem.php?pid=73

代码如下:

[java]  view plain  copy
  1. import java.io.*;    
  2. import java.math.BigInteger;    
  3. import java.util.*;    
  4.     
  5. public class Main    
  6. {    
  7.     public static void main(String args[])    
  8.     {    
  9.         Scanner cin = new Scanner(System.in);       
  10.         while(cin.hasNext())    
  11.         {    
  12.             BigInteger a = cin.nextBigInteger();    
  13.             BigInteger b = cin.nextBigInteger();    
  14.             if(a.equals(BigInteger.ZERO) && b.equals(BigInteger.ZERO))    
  15.                 break;    
  16.             int flag = a.compareTo(b);    
  17.             if(flag == -1)    
  18.                 System.out.println("a<b");    
  19.             else if(flag == 0)    
  20.                 System.out.println("a==b");    
  21.             else    
  22.                 System.out.println("a>b");    
  23.         }    
  24.     }    
  25. }    

大数加法

题目链接:http://acm.nyist.net/JudgeOnline/problem.php?pid=103

代码如下:

[java]  view plain  copy
  1. import java.math.BigInteger;    
  2. import java.util.*;    
  3. import java.io.*;    
  4.     
  5. public class Main    
  6. {    
  7.     public static void main(String args[])    
  8.     {    
  9.         Scanner in = new Scanner(System.in);    
  10.         int n = in.nextInt();           
  11.         for(int i = 1; i <= n; ++i)    
  12.         {    
  13.             BigInteger a = in.nextBigInteger();    
  14.             BigInteger b = in.nextBigInteger();    
  15.             BigInteger ans = a.add(b);    
  16.             System.out.println("Case " + i + ":");    
  17.             System.out.println(a + " + " + b + " = " +ans);    
  18.         }    
  19.     }    
  20. }    

递推求值

题目链接:http://acm.nyist.net/JudgeOnline/problem.php?pid=114

代码如下:

[java]  view plain  copy
  1. import java.io.*;    
  2. import java.math.BigInteger;    
  3. import java.util.*;    
  4.     
  5. public class Main    
  6. {    
  7.     public static void main(String args[])    
  8.     {    
  9.         Scanner cin = new Scanner(System.in);       
  10.         BigInteger a[] = new BigInteger[100];    
  11.         while(cin.hasNext())    
  12.         {    
  13.             for(int i = 0; i <= 2; ++i)    
  14.                 a[i] = cin.nextBigInteger();    
  15.             for(int i = 3; i <= 99; ++i)    
  16.                 a[i] = a[i - 1].add(a[i - 2]).add(a[i - 3]);    
  17.             System.out.println(a[99]);    
  18.         }    
  19.     }    
  20. }    

高精度幂

题目链接:http://acm.nyist.net/JudgeOnline/problem.php?pid=155

代码如下:

[java]  view plain  copy
  1. import java.io.*;    
  2. import java.math.BigDecimal;    
  3. import java.util.*;    
  4.     
  5. public class Main    
  6. {    
  7.     public static void main(String args[])    
  8.     {    
  9.         Scanner cin = new Scanner(System.in);       
  10.         while(cin.hasNext())    
  11.         {    
  12.             BigDecimal ans = cin.nextBigDecimal();    
  13.             int n = cin.nextInt();    
  14.             String res = ans.pow(n).stripTrailingZeros().toPlainString(); //整数去掉小数点和后面的0    
  15.             if(res.startsWith("0")) //去掉前导0    
  16.             {    
  17.                 res = res.substring(1);    
  18.             }    
  19.             System.out.println(res);    
  20.         }    
  21.     }    
  22. }    

大数运算:

[java]  view plain  copy
  1. import java.util.*;  
  2. import java.math.*;  
  3. public class Main{  
  4.     public static void main(String args[]){  
  5.        Scanner cin = new Scanner(System.in);  
  6.        BigInteger a, b;  
  7.         
  8.        //以文件EOF结束  
  9.        while (cin.hasNext()){  
  10.            a = cin.nextBigInteger();  
  11.            b = cin.nextBigInteger();  
  12.             
  13.            System.out.println(a.add(b)); //大整数加法  
  14.            System.out.println(a.subtract(b)); //大整数减法  
  15.            System.out.println(a.multiply(b)); //大整数乘法  
  16.            System.out.println(a.divide(b)); //大整数除法(取整)  
  17.            System.out.println(a.remainder(b)); //大整数取模  
  18.             
  19.            //大整数的比较  
  20.            if( a.compareTo(b) == 0 ) System.out.println("a == b"); //大整数a==b  
  21.            else if( a.compareTo(b) > 0 ) System.out.println("a > b"); //大整数a>b  
  22.            else if( a.compareTo(b) < 0 ) System.out.println("a < b"); //大整数a<b  
  23.             
  24.            //大整数绝对值  
  25.            System.out.println(a.abs()); //大整数a的绝对值  
  26.             
  27.            //大整数的幂  
  28.            int exponent=10;  
  29.            System.out.println(a.pow(exponent)); //大整数a的exponent次幂  
  30.             
  31.            //返回大整数十进制的字符串表示  
  32.            System.out.println(a.toString());  
  33.             
  34.            //返回大整数p进制的字符串表示  
  35.            int p=8;  
  36.            System.out.println(a.toString(p));  
  37.        }  
  38.     }  
  39. }  


大数:

java大数(2013长春网络赛)--hdu4762
总结一下:
1.java提交类要写Main。
2.读取大数。

1 Scanner read=new Scanner(System.in);  
2 BigInteger m;  
3 m=read.nextBigInteger(); 

3.基本类型转化成大数。

1 BigInteger q=BigInteger.valueOf(n);

4.大数最大公约数:

1 BigInteger a=p.gcd(q);  

5.finally函数只能写在try-catch后面

复制代码
 1 import java.math.*;
 2 import java.util.*;
 3 public class Main {
 4 
 5     /**
 6      * @param args
 7      */
 8     public static void main(String[] args) {
 9         // TODO Auto-generated method stub
10         Scanner read=new Scanner(System.in);
11         try{
12         int t;
13         BigInteger m;
14         int n;
15         t=read.nextInt();
16         while(t-->0){
17             m=read.nextBigInteger();
18             n=read.nextInt();
19             BigInteger p=m.pow(n-1);
20             BigInteger q=BigInteger.valueOf(n);
21             BigInteger a=p.gcd(q);
22             System.out.println(q.divide(a)+"/"+p.divide(a));
23             }
24         }
25         finally{
26             read.close();
27         }
28         
29     }    
30 }

计算大实数R^n的值。


[java]  view plain  copy
  1. import java.io.*;  
  2. import java.math.*;  
  3. import java.util.*;  
  4. import java.text.*;  
  5.   
  6. public class Main {  
  7.     public static void main(String[] args) {  
  8.         Scanner cin = new Scanner(System.in);  
  9.         BigDecimal num;//定义一个大实数  
  10.         int ep,sta, end, i;  
  11.         String st;  
  12.         while(cin.hasNext()/*相当于C++的!=EOF*/) {  
  13.             num = cin.nextBigDecimal(); //底数  
  14.             ep = cin.nextInt();    //指数  
  15.             num = num.pow(ep);      //计算num^ep  
  16.             st = new String(num.toPlainString()); //转为字符串  
  17.             sta = 0;  
  18.             while(st.charAt(sta) == '0') sta++;   //去掉前缀的0  
  19.             end = st.length() -1;  
  20.             while(st.charAt(end)=='0') end--;  //去掉后缀的0  
  21.             if(st.charAt(end)=='.') end--;     //若小数点后没0,去掉  
  22.             for(i=sta; i<=end; i++)  
  23.                 System.out.print(st.charAt(i));  
  24.             System.out.println();  
  25.         }  
  26.         System.exit(0);  
  27.     }  
  28. }  
在二进制中,2的算术平方根,即sqrt(2),是一个无限小数1.0110101000001001111...
给定一个整数n和一个01串S,你的任务是在sqrt(n)的小数部分(即小数点之后的部分)中找到S第一次出现的位置。如果sqrt(n)是整数,小数部分看作是无限多个0组成的序列。
 

Input

输入第一行为数据组数T (T<=20)。以下每行为一组数据,仅包含一个整数n (2<=n<=1,000,000)和一个长度不超过20的非空01串S。

Output

对于每组数据,输出S的第一次出现中,第一个字符的位置。小数点后的第一个数字的位置为0。输入保证答案不超过100。

Sample Input

2
2 101
1202 110011

Sample Output

2
58
import java.util.*;
import java.math.*;

public class Main {

    public static void main(String[] args) 
    {
        Scanner cin  = new Scanner(System.in);
        int T = cin.nextInt();
        for(int t=1;t<=T;t++)
        {
            int num = cin.nextInt();
            String str = cin.next();
            
            BigDecimal d = Sqrt(num);//sqrt(num),最多保存150位。
            BigInteger x =d.toBigInteger();//取整数部分
            d = d.subtract(BigDecimal.valueOf(x.longValue()));//保留小数部分
            String hxl = Tostring(d);//将小数部分转化为二进制数字。
            int tom = hxl.indexOf(str);//查找第一次出现的位置。
            if(tom==-1) tom = 0;
            System.out.println(tom);    
        }
    }

    private static String Tostring(BigDecimal d) 
    {
        /**
         * 将小数部分转化为二进制数字。
         * 每次乘2,取整数部分就可以。
         * 长度最长为150.
         * 如果数字太大会超时的。如果数字太小,会不精确。
         */
        String tom = new String();
        int n = 150;
        while(!d.equals(BigDecimal.ZERO)&& (n--)!=0)
        {
            d = d.multiply(BigDecimal.valueOf(2));
            BigInteger x = d.toBigInteger();
            tom+=x;
            d = d.subtract(BigDecimal.valueOf(x.longValue()));
        }
        return tom;
    }

    private static BigDecimal Sqrt(int num) {
        /***
         * 将整数num模拟开根号。
         */
        BigDecimal sqrtNum = BigDecimal.valueOf(0);
        boolean isFindSqrt = false;
        
        int key = (int)Math.sqrt(num*1.0);
        sqrtNum = BigDecimal.valueOf(key*1.0);
        if(key*key == num) isFindSqrt = true;
        if(!isFindSqrt)//不能刚好整除,调用函数,获得精确值。
        {
            sqrtNum = recuFindSqrt(num,BigDecimal.valueOf(key),
                    isFindSqrt,BigDecimal.valueOf(1));
        }
        return sqrtNum;
    }
    private static BigDecimal recuFindSqrt(int num, BigDecimal sqrtValue,
            boolean isFindSqrt, BigDecimal ac) {
        /**
         * 从每一位开始寻找,从0--9枚举。找最接近的点的方法。
         * 暴力。(⊙o⊙)…
         */
        ac = ac.multiply(BigDecimal.valueOf(10));
        BigDecimal tempSqrt = BigDecimal.valueOf(0);
        
        for(int i=0;i<10;i++)
        {
            tempSqrt = sqrtValue.add(BigDecimal.valueOf(i).divide(ac));
            if(tempSqrt.multiply(tempSqrt).equals(BigDecimal.valueOf(num)))
            {
                isFindSqrt = true;
                sqrtValue = tempSqrt;
            }else if(tempSqrt.multiply(tempSqrt).compareTo(BigDecimal.valueOf(num))==1)
            {
                tempSqrt = sqrtValue.add(BigDecimal.valueOf(i-1).divide(ac));
                sqrtValue = tempSqrt;
                break;
            }
        }
        BigDecimal temp = tempSqrt;
        if(temp.toString().length()<=150 && isFindSqrt==false)
        {
            sqrtValue = recuFindSqrt(num, tempSqrt, isFindSqrt, ac);
        }
        return sqrtValue;
    }
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值