suppose String S1 = "19238248931244327089";
S2 = "43109431029897431"; the value of the number required to obtain s1+s2
cannot be added using the class library.
suppose String S1 = "19238248931244327089";
S2 = "43109431029897431"; the value of the number required to obtain s1+s2
cannot be added using the class library.
1, use BigInteger class, BigDecimal class
public static void main(String[] args) {
String a="19238248931244327089";
String b="43109431029897431";
String str=new BigInteger(a).add(new BigInteger(b)).toString();
System.out.println(str);
}
2, reverse string, align string vacancy 0, add two positive integers
public static String add(String n1,String n2){
String result="";
//
String num1=new StringBuffer(n1).reverse().toString();
String num2=new StringBuffer(n2).reverse().toString();
int len1=num1.length();
int len2=num2.length();
int maxLen=len1>len2?len1:len2;
//()
int nSum[]=new int[maxLen+1];
boolean nOverFlow=false;
//
if(len1<len2){
for (int i = len1; i < len2; iPP) {
num1+="0";
}
}else if(len1>len2){
for (int i = len2; i < len1; iPP) {
num2+="0";
}
}
//
for (int i = 0; i < maxLen; iPP) {
//
if (nOverFlow) {
nSum[i]=Integer.parseInt(num1.charAt(i)+"")+
Integer.parseInt(num2.charAt(i)+"")+1;
}else{
nSum[i]=Integer.parseInt(num1.charAt(i)+"")+
Integer.parseInt(num2.charAt(i)+"");
}
//
nOverFlow=handleSumOverTen(nSum,i);
}
//
if(nOverFlow) {
nSum[maxLen] = 1;
}else {
nSum[maxLen] =0 ;
}
for (int i = 0; i < nSum.length; iPP) {
result+=String.valueOf(nSum[i]);
}
String result1=new StringBuffer(result).reverse().toString();
return result1;
}
private static boolean handleSumOverTen(int[] nSum, int i) {
boolean flag = false;
if(nSum[i] >= 10) {
nSum[i] = nSum[i] - 10;
flag = true;
}
else {
flag = false;
}
return flag;
}
public static void main(String[] args) {
String num=add("19238248931244327089", "43109431029897431");
System.out.println(num);
}
this result may have an extra 0 characters
3, complete the string (use the insert method in StringBuffere to insert len zeros where the string index is 0), align and add
public class test {
public static void main(String[] args) {
int[] result = bigNumSum("19238248931244327089", "43109431029897431");
for(int i=0; i < result.length; iPP) {
System.out.print(result[i]);
}
}
public static int[] bigNumSum(String num1, String num2) {
String number1 = num1;
String number2 = num2;
int len1=number1.length();
int len2=number2.length();
int len=Math.abs(len1-len2);
char insertNum[]=new char[len];
for (int i = 0; i < insertNum.length; iPP) {
insertNum[i]='0';
}
String str1="";
String str2="";
//
if (len1<len2) {
str1=new StringBuffer(number1).insert(0, insertNum).toString();
str2=number2;
}else if(len1>len2){
str1=number1;
str2=new StringBuffer(number2).insert(0, insertNum).toString();
}
//
char[] ch1 = str1.toCharArray();
char[] ch2 = str2.toCharArray();
int[] sum;
//true>=10
boolean flag = false;
//+1>10
sum = new int[ch1.length+1];
//
for(int i=ch1.length-1; i>=0; i--) {
//11
if(flag) {
//
sum[i+1] = (int)(ch1[i] - '0') + (int)(ch2[i] - '0') + 1;
}else {
sum[i+1] = (int)(ch1[i] - '0') + (int)(ch2[i] - '0');
}
flag = handleSumOverTen(sum, i); //>10
}
handleTopDigit(flag, sum); //
return sum;
}
/*
* >10
*/
public static boolean handleSumOverTen(int[] sum, int i) {
boolean flag = false;
if(sum[i+1] >= 10) {
sum[i+1] = sum[i+1] - 10;
flag = true;
}
else {
flag = false;
}
return flag;
}
/*
*
*/
public static void handleTopDigit(Boolean flag, int[] sum) {
if(flag) {
sum[0] = 1;
}else {
sum[0] = 0;
}
}
}
4. This method is basically consistent with the three methods, but the difference is that the three methods do not need to judge step by step because the lengths of two strings are equal by filling 0, but the overall running efficiency is still high
.public class test {
public static void main(String[] args) {
int[] result = bigNumSum("19238248931244327089", "43109431029897431");
for(int i=0; i < result.length; iPP) {
System.out.print(result[i]);
}
}
public static int[] bigNumSum(String num1, String num2) {
String number1 = num1;
String number2 = num2;
//
char[] ch1 = number1.toCharArray();
char[] ch2 = number2.toCharArray();
int[] sum;
//
int len = Math.abs(ch1.length - ch2.length);
//true>=10
boolean flag = false;
//
if(ch1.length == ch2.length) {
//+1>10
sum = new int[ch1.length+1];
//
for(int i=ch1.length-1; i>=0; i--) {
//11
if(flag) {
//
sum[i+1] = (int)(ch1[i] - '0') + (int)(ch2[i] - '0') + 1;
}else {
sum[i+1] = (int)(ch1[i] - '0') + (int)(ch2[i] - '0');
}
flag = handleSumOverTen(sum, i, len); //>10
}
handleTopDigit(flag, sum); //
return sum;
}
else if(ch1.length > ch2.length) { //12
sum = new int[ch1.length+1]; //1+1
for(int i=ch2.length-1; i>=0; i--) {
if(flag) {
sum[i+len+1] = (int)(ch1[i+len] - '0') + (int)(ch2[i] - '0') + 1;
}
else {
sum[i+len+1] = (int)(ch1[i+len] - '0') + (int)(ch2[i] - '0');
}
flag = handleSumOverTen(sum, i, len);
}
for(int i=ch1.length-ch2.length-1; i>=0; i--) { //1
if(flag) {
sum[i+1] = (int)(ch1[i] - '0') + 1;
}
else {
sum[i+1] = (int)(ch1[i] - '0');
}
flag = handleSumOverTen(sum, i, 0);
}
handleTopDigit(flag, sum);
return sum;
}
else {
sum = new int[ch2.length+1];
for(int i=ch1.length-1; i>=0; i--) {
if(flag) {
sum[i+len+1] = (int)(ch1[i] - '0') + (int)(ch2[i+len] - '0') + 1;
}
else {
sum[i+len+1] = (int)(ch1[i] - '0') + (int)(ch2[i+len] - '0');
}
flag = handleSumOverTen(sum, i, len);
}
for(int i=ch2.length-ch1.length-1; i>=0; i--) {
if(flag) {
sum[i+1] = (int)(ch2[i] - '0') + 1;
}
else {
sum[i+1] = (int)(ch2[i] - '0');
}
flag = handleSumOverTen(sum, i, 0);
}
handleTopDigit(flag, sum);
return sum;
}
}
/*
* >10
*/
public static boolean handleSumOverTen(int[] sum, int i, int len) {
boolean flag = false;
if(sum[i+len+1] >= 10) {
sum[i+len+1] = sum[i+len+1] - 10;
flag = true;
}
else {
flag = false;
}
return flag;
}
/*
*
*/
public static void handleTopDigit(Boolean flag, int[] sum) {
if(flag) {
sum[0] = 1;
}else {
sum[0] = 0;
}
}
}
encounters a javascript algorithm problem, which requires that find out all prime factors of a number algorithm I designed: function primeFactors(n){ var factors = []; var divisor = 3; if (n % 2==0) factors.push(2); while(n>5){ ...
for example, if you want to move from 0 to 100 1px at this speed, you can move x coordinates + 1 per frame. What if you want to move obliquely from 0 ~ 0 to 100 ~ ~ 20 at this speed? ...
Let the bullets be fired according to the direction of the mouse click, just like those who are good at fishing ...
find out how many zeros there are between 1 and 1 million ...
topic: ss for example: enter google, to find that the longest symmetrical string is goog enter abcda to find that the longest symmetric string is aba. if there is more than one longest symmetric string, multiple longest symmetric strings o...
encounters an algorithm; similar to var a = [1 br > how to deal with it into a two-digit array [[1], [2], [4Power4], [5Power5]] < beacon > I wrote it myself: uh. how to deal with it into a two-digit array [[1], [2], [4parry 4], [5pjre 5]]. Loop twice, n...
has a select drop-down box with five option elements with value values from 1 to 5 <select> <option value="1">1< option> <option value="2">2< option> <option value="3">3< ...
given a non-empty array of integers, each element appears twice except for an element that appears only once. Find the element that only appears once. example 1: : [2,2,1] : 1 example 2: : [4,1,2,1,2] : 4 the answer with the highest running effici...
let arrs=[102,233,45,350,130,220,195,240] I want arrs to split into two groups, but I want the sum of the two groups to be as close as possible. The smaller the difference, the better. My own idea is to re-sort, from small to large, and then judge the s...
I want to output all possible routes, but in the end, I only output one route. I haven t figured out Orz for a long time. the following is the source code: ma mbmb.fill(0); 0 1 (x0,y0) position=[] direction=[[-1,0],[0,-1],[1,0],[0,1...
use JavaScript to build a segment tree, and recursion is used in the construction process. This should make the call stack overflow. I wonder if there is any other way to solve this problem. < H1 > Code < H1 > ...
problem description there is an example, given a linked list public class ListNode { int val; ListNode next; ListNode(int x) { val = x; } } I hope to calculate the sum of linked list elements by ordinary recursion and tail recursion, and...
data A: [ { "id":340, "name":"", "sub":[ { "id":341, "name":"" }, { &q...
for example: [1, 2, 3, 4, 5] after sorting: 1, 3, 5, 2, 4 Note: does not create a new array , that is, it is changed on the basis of the original array. ...
php algorithm-- catching water droplets A novice PHP programmer, who has been on the job for half a year and is still hovering in endless additions, deletions, modifications and data optimization, has recently been brought into the algorithm pit by th...
directory tree initialization initially returns only a two-tier structure. [ { name: , path: , id: 1, childs: 2, children: [ { name: 01 , path: 01 , id: 2, ...
recently, when I looked at JCF, I found this sentence on the Internet. when searching a large amount of information, TreeSet is more efficient than ArrayList and can guarantee the completion of within the time of log (n). Treeset is a tree structure...
recently, when I looked at JCF, I found this sentence on the Internet. when searching a large amount of information, TreeSet is more efficient than ArrayList and can guarantee the completion of within the time of log (n). Treeset is a tree structure...
there are now such data objects arr = [ {] start: 1, end: 12 }, { start: 2, end: 5 }, { start: 6, end: 10 }, . ] how to determine whether the start and end of each object are continuous for example, the above situation is incorrect, because 1-12 c...
What is the data structure corresponding to Object in js? What is its logical structure and physical structure? ...