• 大数加法


void trs(string a,vector<int>& v)//一位一位的计算

{

v.clear();

int p =0;

for (int i = a.size() -1;
i >= 0; i --) {

p = a[i] -'0';

v.push_back(p);

}

}

&b)

{

vector<int> c;

int p =
0;

for (int i =
0; ; i ++) {

if (i >= a.size() && i >= b.size()) {

c.push_back(p);

break;

}

if(i < a.size())

{

p += a[i];

}

if (i < b.size()) {

p += b[i];

}

if (p >=
10) {

c.push_back(p %
10);

p /= 10;

}

else{

c.push_back(p);

p = 0;

}

}

p = c.size() -
1;

while (c[p] ==
0) {

p --;

}

if (p <
0) {// 0 + 0 = 0

printf("0");

}

for (int i = p; i >=
0 ; i --) {

printf("%d",c[i]);

}

printf("\n");

}

#include <iostream>

#include <string>

#include <vector>

using namespacestd;

const int base =100000000;//8位8位的算

void pf(int t)

{

int b =base;

while (t < b /10) {

cout <<0 ;

b /= 10;

}

cout << t ;

}

void trs(string s,vector<int> &va)//应该从低位开始划分

{

va.clear();

int p = (s.size() -1 ) /
8 +1;//

int tmp =0;

for (int i =1; i <= p; i ++) {

tmp = 0;

int srt =max(0 , (int)s.size()
-8 * i );

for (int j =0; j <
8 && srt + j < s.size() -8 * i +
8; j ++) {

tmp = tmp * 10 + s[srt +j] -
'0';

}

va.push_back(tmp);

}

}

&vb)

{

vector<int> vc;

for (int i =0,tmp =
0; ; i ++) {

if (i >= va.size() && i >= vb.size() && tmp ==0)
{

break;

}

int p = tmp;

if (i < va.size()) {

p += va[i];

}

if (i < vb.size()) {

p += vb[i];

}

vc.push_back(p %base);

tmp = p / base;//进位

}

cout << vc[vc.size() -1]
;

for (int i = vc.size() -2;
i >= 0; i --) {

pf(vc[i]);//前导0,仅第一个不用补0

}

cout <<endl;

}

int main()

{

int t;

cin >> t;

string a,b;

vector<int> va,vb;

for (int i =1; i <= t; i ++) {

cin >> a >> b;

trs(a, va);

trs(b, vb);

if (i !=1) {

cout <<endl;

}

cout <<"Case " << i <<":" <<endl;

cout << a <<" + " << b <<
" = " ;

}

return0;

}

展开全文

...