大整数相加(java VS c++实现)
大整数相加
??? 整体思想都是补零,如a:123,b:1,补零后a:123, b:001然后相加进位
java是前补零
c++是将数从个位存,剩下的默认为0
java实现
#include<iostream>using namespace std;void main(){char a[201], b[201];int a1[201], b1[201];memset(a1, 0, sizeof(a1));memset(b1, 0, sizeof(b1));cin >> a >> b;int len1 = strlen(a);int len2 = strlen(b);int len = len1 > len2 ? len1 : len2;int j = 0;for(int i = len1 - 1; i >= 0; i --){a1[j ++] = a[i] - '0';//a1从个位开始存放}j = 0;for(i = len2 - 1; i >= 0; i --){b1[j ++] = b[i] - '0';}int n = 0; //n表示进位int temp;for(i = 0; i < len; i ++){temp = a1[i] + b1[i] + n;a1[i] = temp % 10;n = temp / 10;}if(n != 0){//表示最高位相加后的进位cout << n;}for(i = len - 1; i >= 0; i --){//从高位开始输出cout << a1[i];}cout << endl;}
?
?
?