JAVA大数(转)

1、输入

首先要想输入需要先包括:

[java] view plaincopy在CODE上查看代码片派生到我的代码片
 
  1. import java.util.*;   
  2.   
  3.    


我们需要其中的 Scanner类声明的对象来扫描控制台输入。

针对A+B来说:

[java] view plaincopy在CODE上查看代码片派生到我的代码片
 
  1. import java.util.*;  
  2. public class Main  
  3. {  
  4.     public static void main(String [] args)  
  5.     {  
  6.         Scanner cin = new Scanner(System.in);//对于Scanner 类声明对象cin用来扫描控制台输入  
  7.         int a = cin.nextInt();  
  8.         int b = cin.nextInt();  
  9.         System.out.println(a+b);//输出a+b  
  10.         cin.close();  
  11.     }  
  12. }  


读一个整数:  int n = cin.nextInt();       相当于 scanf("%d", &n);  或 cin >> n; 

读一个字符串:String s = cin.next();       相当于 scanf("%s", s);   或 cin >> s;  

读一个浮点数:double t = cin.nextDouble(); 相当于 scanf("%lf", &t); 或 cin >> t;  

读一整行:    String s = cin.nextLine();   相当于 gets(s);          或 cin.getline(...); 

读一个大数:  BigInteger c = cin.nextBigInteger();

 

不过需要注意的是它们没有像scanf一样有判断是否输入到结尾的功能。

 

这时候需要用一个Scanner类的方法hasnext()来判断是否输入到文件结尾;

 

例如POJ2506 这是一个针对大数处理的递推题。用Java就很好解决。

[java] view plaincopy在CODE上查看代码片派生到我的代码片
 
  1. import java.math.*;//大数类在Java.math里面,如果不包括的话声明不了大数对象  
  2. import java.util.*;  
  3.   
  4. public class Main  
  5. {  
  6.     public static void main(String[] args)  
  7.     {  
  8.   
  9.         // TODO Auto-generated method stub  
  10.         /*sss*/  
  11.         BigInteger [] a=new BigInteger[255]; //大数数组  
  12.         a[0]=BigInteger.ONE; //初始化  
  13.         a[1]=BigInteger.ONE;  
  14.         a[2]=a[1].add(a[0].add(a[0]));  
  15.         for(int i=3;i<=250;i++)  
  16.         {  
  17.             a[i]=a[i-1].add(a[i-2].add(a[i-2]));  
  18.         }  
  19.         Scanner cin = new Scanner(System.in); //输入打开  
  20.           
  21.         while(cin.hasNext())//判断是否文件结束  
  22.         {  
  23.             int n=cin.nextInt();  
  24.             System.out.println(a[n]); //输出  
  25.         }  
  26.         cin .close();//输入关闭  
  27.     }  
  28. }   



2、输出:

[java] view plaincopy在CODE上查看代码片派生到我的代码片
 
  1. public class Main {  
  2.     public static void main(String[] args)  
  3.     {  
  4.   
  5.         // TODO Auto-generated method stub  
  6.           
  7.         double d;  
  8.           
  9.         d=9999.99999;  
  10.           
  11.         System.out.format("%f",d);                //9999.999990     没有回车  
  12.           
  13.         System.out.format("%10.10f",d).println(); //9999.9999900000 输出回车  
  14.           
  15.         System.out.format("%.4f",d).println();    //10000.0000      输出回车  
  16.           
  17.         System.out.format("%3.4f",d).println();   //10000.0000      输出回车  
  18.           
  19.         System.out.println(d);                    //输出当前变量      输出回车  
  20.           
  21.         System.out.println();                     //                输出回车  
  22.           
  23.         System.out.printf("%f",d);                //9999.999990     没有回车  
  24.           
  25.         System.out.print(d);                      //9999.99999      没有回车  
  26.           
  27.     }  
  28. }  



当然,输出的时候也有对小数位数处理的方法:

0代表当前位向后都是0就输出0占位,#代表若当前位向后都是0就不输出这些位上的数字。

[java] view plaincopy在CODE上查看代码片派生到我的代码片
 
  1. import java.text.DecimalFormat;  
  2. public class Main {  
  3.     public static void main(String[] args)  {  
  4.         double num = 9.999;  
  5.         DecimalFormat p3 = new DecimalFormat("#.00#");  
  6.         DecimalFormat p4 = new DecimalFormat("#.000#");  
  7.         DecimalFormat p5 = new DecimalFormat("#.0000#");  
  8.         System.out.println(p3.format(num));//输出9.999  
  9.         System.out.println(p4.format(num));//输出9.999  
  10.         System.out.println(p5.format(num));//输出9.9990  
  11.   
  12.           
  13.     }  
  14. }  



3、高精度

对于大数来说,Java提供了BigDecimal和BigInteger两个类,都包含在java.math.*里面。

BigInteger是大整形类,BigDecimal是大浮点型类。

这两个类产生的对象没有数据范围,只要你的电脑内存足够就可以。

下面用BigInteger举个例子:

[java] view plaincopy在CODE上查看代码片派生到我的代码片
 
  1. import java.math.BigInteger;  
  2.   
  3. public class Main {  
  4.       
  5.     public static void main(String[] args)  
  6.       
  7.     {  
  8.           
  9.         int a=6,b=3;  
  10.           
  11.         BigInteger x,y,z;  
  12.           
  13.         x=BigInteger.valueOf(a);           //转化赋值  
  14.           
  15.         y=BigInteger.valueOf(b);  
  16.           
  17.         System.out.println(x.add(y));      //加  
  18.           
  19.         System.out.println(x.subtract(y)); //减  
  20.           
  21.         System.out.println(x.multiply(y)); //乘  
  22.           
  23.         System.out.println(x.divide(y));   //除  
  24.           
  25.         System.out.println(x.mod(y));      //取余  
  26.     }  
  27. }  

4、进制转换

java一直就是作弊器一般的存在,就像它的进制转换函数一样:

[java] view plaincopy在CODE上查看代码片派生到我的代码片
 
  1. public class Main {  
  2.       
  3.     public static void main(String[] args)  
  4.       
  5.     {  
  6.         String string;  
  7.         int a=8;  
  8.         int x=2;  
  9.         string = Integer.toString(a, x);    //把int型数据转换乘X进制数并转换成string型  
  10.         System.out.println(string);  
  11.           
  12.         int b = Integer.parseInt(string, x);//把字符串当作X进制数转换成int型  
  13.         System.out.println(b);  
  14.     }  
  15. }  

5、字符串

Java中的String   和char数组是完全不同的两种东西。

String中的字符是不能在原位置改变的,要改变必须改变并保存到新的String里,或者保存到char 数组里再修改

[java] view plaincopy在CODE上查看代码片派生到我的代码片
 
  1. import java.io.*;  
  2. import java.util.*;  
  3. public class Main  
  4. {  
  5.     public static void main(String[] args)   
  6.     {  
  7.         Scanner cin = new Scanner (new BufferedInputStream(System.in));  
  8.         String st = "abcdefg";  
  9.         System.out.println(st.charAt(0)); // st.charAt(i)就相当于st[i].  
  10.         char [] ch;  
  11.         ch = st.toCharArray(); // 字符串转换为字符数组.  
  12.         for (int i = 0; i < ch.length; i++){  
  13.             ch[i] += 1;//字符数组可以像C++   一样操作  
  14.         }  
  15.         System.out.println(ch); // 输入为“bcdefgh”.  
  16.         st = st.substring(1); // 则从第1位开始copy(开头为第0位).  
  17.         System.out.println(st);  
  18.         st=st.substring(2, 4);  //从第2位copy到第4位(不包括第4位)  
  19.         System.out.println(st);//输出“de”  
  20.     }  
  21. }  
原文地址:https://www.cnblogs.com/chenhuan001/p/3448342.html