java實現大數字的加減乘除
阿新 • • 發佈:2018-12-22
java程式碼實現 大數字的 加減乘除
一:加法
基本上是模擬了人工的演算法,比如1234+987
a.先把位數補齊一致,變成:1234 + 0987
b.從各位開始運算,每次用一個變數記錄是否要進位
c.遍歷所有的位置就完成了加法;
程式碼如下:`
public class 大數加法 { public static void main(String[] args) { //輸入2個字串型別的大數字 String s1 = new Scanner(System.in).nextLine(); String s2 = new Scanner(System.in).nextLine(); String result=add(s1,s2); System.out.println(result); } private static String add(String s1, String s2) { //保證s1小於等於s2的長度 if(s1.length()>s2.length()){ String t=s1; s1=s2; s2=t; } int cha=s2.length()-s1.length(); for (int i = 0; i < cha; i++) { s1='0'+s1; //把s2前面的空缺補上0 } String result=""; int w=0; //定義一個進位的變數 for (int i = s2.length()-1; i >=0 ; i--) { //從s1和s2中取出字元,減去48就是 int型別的數字,再加上進位就是當前位的結果 int c=s2.charAt(i)+s1.charAt(i)-96+w; w=c/10; //把當前計算結果整除10就是 w的進位 result=(c%10) + result; //取餘就是 當前位應該顯示的數字,把它加在前面就可以了 } //因為我們的迴圈沒有判斷第一位的進位 ,所以最後再判斷一次 if(w==1)result=1+result; return result; } }
二。減法
減法和加法類似,也是模擬人工計算
程式碼如下
public class 大數的減法 { public static void main(String[] args) { String s1 = new Scanner(System.in).nextLine(); String s2 = new Scanner(System.in).nextLine(); String result = jian(s1, s2); System.out.println(result); } private static String jian(String s1, String s2) { String fuhao = ""; int l1 = s1.length(); int l2 = s2.length(); //判斷,如果s1小於s2那麼 符號就是 - 然後,s1和s2交換位置,保證s1是大的 if ((l1 == l2 && s1.compareTo(s2) < 0) || l1 < l2) { fuhao = "-"; String t = s1; s1 = s2; s2 = t; } for (int i = 0; i < Math.abs(l1-l2); i++) { s2='0'+s2; //補0處理,使長度一致 } String result=""; int w=0; for (int i = s1.length()-1; i >=0 ; i--) { //計算每一個位置的差,在加上借位w的值 int c=s1.charAt(i)-s2.charAt(i)+w; //如果c小於0,說明需要借位,c+=10,然後w該為-1,否則,借位w=0 if(c<0){ c+=10; w=-1; }else{ w=0; } result=c+result; // 把當前位的數字放入result裡 } return fuhao+result; } }
3.乘法
乘法也是模擬人工計算,就是把其中一個數字,拆分成一位數,然後累和
例如 123* 45 就是123✖40+123✖5 其中加法用的是上面的add方法
程式碼如下:
import java.util.Scanner; public class 大數的乘法 { public static void main(String[] args) { //輸入2個數字 String s1 = new Scanner(System.in).nextLine(); String s2 = new Scanner(System.in).nextLine(); String result=muilt(s1,s2); System.out.println(result); } private static String muilt(String s1, String s2) { String result=""; //把s2拆分成一個一個的 for (int i = 0; i < s2.length(); i++) { //計算s2中單個數字和s1的乘積 String temp=per(s1,s2.charAt(i)); //把每次計算的乘積想加(因為每次的乘積位置不一樣,所以先要補0) result=add(result,add_0(temp,s2.length()-1-i)); } return result; } //單個數字和s1的積 private static String per(String s1, char c) { int n=c-'0'; String result=""; int w=0; for (int i = s1.length()-1; i >=0; i--) { //計算當前位置的積 int m=(s1.charAt(i)-'0')*n+w; // m/10就是進位值 w=m/10; // m%10就是當前位置的值 result=m%10+result; } //對第一位進行判斷 if(w!=0)result=w+result; return result; } //大數和的方法 private static String add(String s1, String s2) { //保證s1小於等於s2的長度 if(s1.length()>s2.length()){ String t=s1; s1=s2; s2=t; } int cha=s2.length()-s1.length(); for (int i = 0; i < cha; i++) { s1='0'+s1; } String result=""; int w=0; for (int i = s2.length()-1; i >=0 ; i--) { int c=s2.charAt(i)+s1.charAt(i)-96+w; w=c/10; result=(c%10) + result; } if(w==1)result=1+result; return result; } private static String add_0(String temp, int i) { for (int j = 0; j < i; j++) { temp=temp+'0'; } return temp; } }
**
4.除法
**大數的除法和模擬人工不一樣,
基本思路:例如 m/n
a.首先令temp=10kn ,並且temp<m, k取最大
b.然後m=m-temp ,當m<temp時停止,記錄執行的次數i,和此時的k
c.當前次的result=i00…(k個0)
d.迴圈執行直到m<n,把result累加即可
如: 128/3
a. temp=30,k=1
b.128-30=98 i=1;98-30=68 i=2; 68-30=38 i=3; 38-30=8 i=4;8-30<0結束;
c;result=40(i=4,k=1)
d:temp=3,k=0
e:8-3=5 i=1; 5-3=2 i=2 ; 2-3<0 結束
f:result=40+2(i=2,k=0)
所以結果是42
import java.util.Scanner;
public class 大數的除法 {
public static void main(String[] args) {
String s1 = new Scanner(System.in).nextLine();
String s2 = new Scanner(System.in).nextLine();
//如果s1<s2那麼就不運算了,直接返回0
if(!check(s1,s2)){
System.out.println(0);
return;
}
String result = chufa(s1, s2);
System.out.println(result);
}
private static boolean check(String s1, String s2) {
if(s1.length()<s2.length() || (s1.length()==s2.length() && s1.compareTo(s2)<0)){
return false;
}
return true;
}
private static String chufa(String s1, String s2) {
String result="";
//迴圈直到s1<s2為止
while(check(s1,s2)){
int n=s1.length()-s2.length();
//判斷需要補多少0 ,如果s1的前m位(m=s2.length())比s2小,那麼只要補n-1個0,否則需要補n個0(n=s1和s2的長度差)
//比如 123/3 , 3需要補成30,是n-1個,而 450/3 ,3需要補成300,是n個0
String num_0=s1.substring(0,s2.length()).compareTo(s2)>=0?get0(n):get0(n-1);
int i=0; //i代表減法執行了幾次,也就是當前次的商
//減法迴圈
while(true){
String m=jian(s1,s2+num_0);
//如果減成負數了,那麼就退出迴圈
if(m.startsWith("-"))break;
//否則 s1重新賦值
s1=m;
i++;
}
//把商補上位數的0,就是當前次的結果,再用 大數加的 方法和前面的數累加
result=add(""+i+num_0,result);
}
return result;
}
//返回 n個0的字串
private static String get0(int n) {
String result="";
for (int i = 0; i < n; i++) {
result+='0';
}
return result;
}
//大數加
private static String add(String s1, String s2) {
//保證s1小於等於s2的長度
if(s1.length()<s2.length()){
s1=get0(s2.length()-s1.length())+s1;
}else{
s2=get0(s1.length()-s2.length())+s2;
}
String result="";
int w=0;
for (int i = s2.length()-1; i >=0 ; i--) {
int c=s2.charAt(i)+s1.charAt(i)-96+w;
w=c/10;
result=(c%10) + result;
}
if(w==1)result=1+result;
return result;
}
//大數減法
private static String jian(String s1, String s2) {
String fuhao = "";
if (!check(s1,s2)) {
fuhao = "-";
String t = s1;
s1 = s2;
s2 = t;
}
s2=get0(Math.abs(s1.length()-s2.length()))+s2;
String result="";
int w=0;
for (int i = s1.length()-1; i >=0 ; i--) {
int c=s1.charAt(i)-s2.charAt(i)+w;
if(c<0){
c+=10;
w=-1;
}else{
w=0;
}
result=c+result;
}
result=result.replaceAll("^0+", "");
return fuhao+result;
}
}