JAvaは大きな数字の加減乗除を実現する
JAvaコードは大きな数字の加減乗除を実現する
一:加算は基本的に人工的なアルゴリズムをシミュレートしたもので、例えば1234+987 a.はまずビット数を一致させ、1234+0987 b.になった.コードは以下の通りです.
二.減算は加算と似ており、シミュレーション人工計算コードも以下の通りである.
3.乗算もアナログ人工計算であり、その中の1つの数字を1桁に分割し、例えば123*45と累積する.✖40+123✖5加算には上のaddメソッドコードが使用されます.
**
4.除算
**大数の除算はシミュレーション人工とは異なり、基本構想:例えばm/n a.はまずtemp=10 knとし、temp b.はm=m-tempとし、m c.の現在のresult= i 00…(k個0)d.サイクルをm例えば: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=38 i=38 i=4;8-30<0終了;c;result= 40(i=4、k=1)d:temp=3、k=0 e:8-3=8=3;8-3=30=38 i=38 i=8 i=3;38-30 i=8 i=4;8 i=4;8-5 i=1;5-3=2 i=2 ; 2-3<0終了f:result=40+2(i=2,k=0)なので結果は42
一:加算は基本的に人工的なアルゴリズムをシミュレートしたもので、例えば1234+987 a.はまずビット数を一致させ、1234+0987 b.になった.コードは以下の通りです.
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.乗算もアナログ人工計算であり、その中の1つの数字を1桁に分割し、例えば123*45と累積する.✖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=10 knとし、temp b.はm=m-tempとし、m c.の現在のresult= i 00…(k個0)d.サイクルをm例えば: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=38 i=38 i=4;8-30<0終了;c;result= 40(i=4、k=1)d:temp=3、k=0 e:8-3=8=3;8-3=30=38 i=38 i=8 i=3;38-30 i=8 i=4;8 i=4;8-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=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()=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;
}
}