Java--大数计算,妈妈再也不用担心我的学习了 .
BigInteger
英文API:
http://docs.oracle.com/javase/8/docs/api/
中文API:
http://tool.oschina.net/apidocs/apidoc?api=jdk-zh
import java.math.BigInteger; import java.util.Scanner; //hdu 1002----A + B Problem II public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int n = sc.nextInt(); int i = 1; while (n-- != 0) { BigInteger a = new BigInteger(sc.next()); BigInteger b = new BigInteger(sc.next()); System.out.println("Case " + (i++) + ":"); System.out.println(a + " + " + b + " = " + a.add(b)); if (n != 0) System.out.println(); } } }
import java.math.BigInteger; import java.util.Scanner; //hdu 1042 N! public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); while (sc.hasNext()) { BigInteger a = new BigInteger(sc.next()); if (a.compareTo(new BigInteger("0")) == 0) { // 如果是0 返回1 System.out.println("1"); continue; } BigInteger sum = a; // 举例 求 9! 这是 sum=a=9 BigInteger b = new BigInteger("1"); while (a.compareTo(b) != 0) { // while a不等于1 // 第一次循环sum=9 sum=sum*8(a=a-1)a=8 // 第二次循环 sum=72,sum=sum*7(a=a-1)a=7 // 第三次循环 sum=504,sum=sum*6(a=a-1)a=6 // .....等于1时退出 sum = sum.multiply(a = a.subtract(b)); } System.out.println(sum); } } }
-C++
1 string add(string str1,string str2) 2 { 3 string str; 4 int len1=str1.length(); 5 int len2=str2.length(); 6 if(len1<len2) { 7 for(int i=1; i<=len2-len1; i++) 8 str1="0"+str1; 9 } else { 10 for(int i=1; i<len1-len2; i++) { 11 str2="0"+str2; 12 } 13 } 14 len1=str1.length(); 15 int cf=0; 16 int tem; 17 for(int i=len1-1; i>=0; i--) { 18 tem=str1[i]-'0'+str2[i]-'0'+cf; 19 cf=tem/10; 20 tem%=10; 21 str=char(tem+'0')+str; 22 } 23 if(cf!=0)str=char(cf+'0')+str; 24 return str; 25 }