//Agata Majewska 08.12.2022 #include <iostream> #include <string.h> using namespace std; int main(){ ios_base::sync_with_stdio(false); cin.tie(NULL); char a[5001]; char b[5001]; char *c; int ca; int cb; int w,m; char *litera; int p=0; int d; cin>>a>>b; int dla = strlen(a); int dlb = strlen(b); if(dla>dlb){ c = new char [dla+1]; c[dla]=0; w = dla; m = dlb; litera = a; } else{ c = new char [dlb+1]; c[dlb]=0; w = dlb; m = dla; litera = b; } for(int i=0;i<w;i++){ if(i<m){ ca = a[dla-i-1] - '0'; cb = b[dlb-i-1] - '0'; //cout<<"\n a = "<<ca<<"\t b = "<<cb<<"\td = "; d=ca+cb+p; //cout<<d; if(d>=10){ p=(d-d%10)/10; d = d%10; } else{ p=0; } //cout<<"\nd = "<<d<<"\tp = "<<p<<"\n"; c[w-i-1]=char(d+48); //cout<<"c["<<w-i-1<<"]="<<c[w-i-1]; } else{ ca = litera[w-i-1] - '0'; d=ca+p; if(d>=10){ p=(d-d%10)/10; d = d%10; } else{ p=0; } c[w-i-1]= char(d%10+48); } } //cout<<"\n"; if(p){ cout<<p; } cout<<c; return 0; }
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 | //Agata Majewska 08.12.2022 #include <iostream> #include <string.h> using namespace std; int main(){ ios_base::sync_with_stdio(false); cin.tie(NULL); char a[5001]; char b[5001]; char *c; int ca; int cb; int w,m; char *litera; int p=0; int d; cin>>a>>b; int dla = strlen(a); int dlb = strlen(b); if(dla>dlb){ c = new char [dla+1]; c[dla]=0; w = dla; m = dlb; litera = a; } else{ c = new char [dlb+1]; c[dlb]=0; w = dlb; m = dla; litera = b; } for(int i=0;i<w;i++){ if(i<m){ ca = a[dla-i-1] - '0'; cb = b[dlb-i-1] - '0'; //cout<<"\n a = "<<ca<<"\t b = "<<cb<<"\td = "; d=ca+cb+p; //cout<<d; if(d>=10){ p=(d-d%10)/10; d = d%10; } else{ p=0; } //cout<<"\nd = "<<d<<"\tp = "<<p<<"\n"; c[w-i-1]=char(d+48); //cout<<"c["<<w-i-1<<"]="<<c[w-i-1]; } else{ ca = litera[w-i-1] - '0'; d=ca+p; if(d>=10){ p=(d-d%10)/10; d = d%10; } else{ p=0; } c[w-i-1]= char(d%10+48); } } //cout<<"\n"; if(p){ cout<<p; } cout<<c; return 0; } |