"Huawei OJ" "Algorithm Total chapter" "Huawei OJ" "039-wireless oss-high-precision integer Addition" "Project Download" topic description
在计算机中,由于处理器位宽限制,只能处理有限精度的十进制整数加减法,比如在32位宽处理器计算机中,参与运算的操作数和结果必须在-231~231-1之间。如果需要进行更大范围的十进制整数加法,需要使用特殊的方式实现,比如使用字符串保存操作数和结果,采取逐位运算的方式。如下:9876543210 + 1234567890 = ?让字符串 num1="9876543210",字符串 num2="1234567890",结果保存在字符串 result = "11111111100"。-9876543210 + (-1234567890) = ?让字符串 num1="-9876543210",字符串 num2="-1234567890",结果保存在字符串 result = "-11111111100"。要求编程实现上述高精度的十进制加法。要求实现方法: public String add (String num1, String num2)【输入】num1:字符串形式操作数1,如果操作数为负,则num1的前缀为符号位‘-‘num2:字符串形式操作数2,如果操作数为负,则num2的前缀为符号位‘-‘【返回】保存加法计算结果字符串,如果结果为负,则字符串的前缀为‘-‘注:(1)当输入为正数时,‘+‘不会出现在输入字符串中;当输入为负数时,‘-‘会出现在输入字符串中,且一定在输入字符串最左边位置;(2)输入字符串所有位均代表有效数字,即不存在由‘0‘开始的输入字符串,比如"0012", "-0012"不会出现;(3)要求输出字符串所有位均为有效数字,结果为正或0时‘+‘不出现在输出字符串,结果为负时输出字符串最左边位置为‘-‘。
Enter a description
输入两个字符串
Output description
输出给求和后的结果
Input example
98765432101234567890
Output example
11111111100
Algorithm implementation
ImportJava.util.Scanner;/** * Author: Wang Junshu * date:2015-12-24 17:18 * All rights Reserved!!! */ Public class Main { Public Static void Main(string[] args) {Scanner Scanner =NewScanner (system.in);//Scanner Scanner = new Scanner (Main.class.getClassLoader (). getResourceAsStream ("Data2.txt")); while(Scanner.hasnext ()) {String n = scanner.next (); String m = Scanner.next ();//"1" Method oneSystem.out.println (Add (n, m));//"2" method two//BigInteger bi1 = new BigInteger (n);//BigInteger Bi2 = new BigInteger (m);//System.out.println (Bi1.add (BI2));} scanner.close (); }/** * Large integers added, n, m are natural number * * @param NS number * @param ms number * @return Results * / Private StaticStringAdd(string ns, string ms) {//NS is positive BooleanPN = Ns.charat (0) !='-';//MS is a positive number BooleanPM = Ms.charat (0) !='-';int[] n;int[] m;if(PN) {n = getnumber (ns); }Else{n = getnumber (ns.substring (1)); }if(PM) {m = GetNumber (ms); }Else{m = GetNumber (Ms.substring (1)); }//Both the same number if(pn = = pm) {//Perform calculations int[] r = Add (M, n); String rs = Tonumber (R);//Add minus sign as needed if(PN) {returnRs }Else{return "-"+ RS; } }Else{the absolute value of//NS is greater than or equal to Ms if(Compare (n, m) >=0) {int[] r = Minus (n, m); String rs = Tonumber (R);//NS is positive, MS is negative if(PN) {returnRs }Else{return "-"+ RS; } }the absolute value of//NS is less than Ms Else{int[] r = Minus (M, n); String rs = Tonumber (R);//NS is positive, MS is negative if(PN) {return "-"+ RS; }Else{returnRs } } } }/** * Two integers added * * @param m integers * @param n integers * @return Results */ Private Static int[]Add(int[] m,int[] n) {//System.out.println (arrays.tostring (n) + "\ n" + arrays.tostring (m)); //guaranteed n not less than M if(M.length > N.length) {int[] t = m; m = n; n = t; }//Maximum length of the result int[] r =New int[N.length +1];//From the low carry intc =0; for(inti =0; i < m.length; i++) {R[i] = M[i] + n[i] + C; c = R[i]/Ten; R[i]%=Ten; }//Calculate the remaining parts for(inti = m.length; i < n.length; i++) {R[i] = N[i] + C; c = R[i]/Ten; R[i]%=Ten; }//System.out.println (arrays.tostring (n) + "\ n" + arrays.tostring (m) + "\ n" + arrays.tostring (r)); //Final and carry if(c! =0) {r[r.length-1] = C;returnR }//No rounding Else{int[] ret =New int[R.length-1]; System.arraycopy (R,0Ret0, ret.length);returnRet } }/** * Compares two integers for equality, subscript from low to high, ignoring leading 0 * * @param m integers * @param n integers * on the most significant bit @return m > N returns 1,m = n returns 0,m < n returns-1 */ Private Static int Compare(int[] m,int[] n) {if(M = =NULL&& n = =NULL) {return 0; }//NULL minimum if(M = =NULL) {return-1; }if(n = =NULL) {return 1; }intLastm = M.length-1;intLASTN = N.length-1;//Find the position of the most significant bit of M, at least one while(Lastm >=1&& m[lastm] = =0) {lastm--; }//Find the position of the most significant bit of N, at least one while(Lastn >=1&& N[LASTN] = =0) {lastn--; }//M has more digits than N, indicating M is greater than n if(Lastm > Lastn) {return 1; }//M with fewer digits than N, indicating m is smaller than n Else if(Lastm < LASTN) {return-1; }Else{///number of digits, compare values on each digit, from high to low for(inti = lastm; I >=0; i--) {if(M[i] > N[i]) {return 1; }Else if(M[i] < n[i]) {return-1; } }return 0; } }/** * Do subtraction n-m, guaranteed n is greater than or equal to M * * @param n integer * @param m integer * @return Result */ Private Static int[]minus(int[] n,int[] m) {n = format (n); m = Format (m);int[] r =New int[N.length];//Current bit is borrow intc =0;intT for(inti =0; i < m.length; i++) {t = N[i]-c-m[i];//Current position is reduced enough if(T >=0) {R[i] = t;//Borrow not performedc =0; }//Not enough to reduce Else{R[i] = t +Ten;//For borrowc =1; } }//And borrow for(inti = m.length; c! =0&& i < n.length; i++) {t = n[i]-C;//Current position is reduced enough if(T >=0) {R[i] = t;//Borrow not performedc =0; }//Not enough to reduce Else{R[i] = t +Ten;//For borrowc =1; } }returnFormat (r); }/** * Integer string is represented as an integer array "contains symbol bit" * * @param n integer String * @return integer array subscript from small to large indicates the number of digits from low to high */< /c1> Private Static int[]GetNumber(String N) {int[] r =New int[N.length ()]; for(inti =0; i < r.length; i++) {R[i] = N.charat (N.length ()-I-1) -' 0 '; }returnR }/** * formatting integers, removing the leading 0 * * @param r integer * @return Results */ Private Static int[]format(int[] r) {intt = r.length-1;//Find the most significant bit while(T >0&& R[t] = =0) {t--; }int[] nr =New int[T +1]; System.arraycopy (R,0Nr0, nr.length);returnNr }/** * Converts an integer represented by an array to a string * * @param r integer * @return string representation of the integer */ Private StaticStringTonumber(int[] r) {if(r = =NULL) {return NULL; } StringBuilder B =NewStringBuilder (r.length); for(inti = r.length-1; I >=0; i--) {b.append (r[i]); }returnB.tostring (); }}
"Huawei OJ" "039-wireless oss-high-precision integer Addition"