fork(2) download
  1. #include<iostream>
  2. #include<cstring>
  3. using namespace std;
  4. int main(void)
  5. {
  6. long int i,j,t,k;
  7. cin>>t;
  8. while(t--)
  9. {
  10. int count1=0;
  11. long int count=0;
  12. char s[10001];
  13. char s1[10001];
  14. char ans[10001];
  15. cin>>s;
  16. cin>>s1;
  17. long int l=strlen(s);
  18. long int l1=strlen(s1);
  19. int temp=0;
  20. //int max1=min(l,l1);
  21. i=l-1,j=l1-1;
  22. k=0;
  23. if(l==l1)
  24. {
  25. temp=1;
  26. while(i>=0 && j>=0)
  27. {
  28. int d=(s[i]-'0')+(s1[j]-'0');
  29. int x=d%10;
  30. ans[k]=(char)(x+'0');
  31. i--;
  32. j--;
  33. k++;
  34. }
  35. ans[k]='\0';
  36. //for(i=k-1;i>=0;i--)
  37. //cout<<ans[i];
  38. //continue;
  39. }
  40. else if(l>l1)
  41. {
  42. int f=l-l1;
  43. for(i=0,j=0,k=0;i<l && j<l1 && k<l;i++,k++)
  44. {
  45. //count++;
  46. if(count!=f)
  47. {ans[k]=s[i];
  48. count++;
  49. }
  50. else
  51. {int d=(s[i]-'0')+(s1[j]-'0');
  52. int x=d%10;
  53. ans[k]=(char)(x+'0');
  54. j++;
  55. }
  56. //count++;
  57. }
  58. ans[k]='\0';
  59.  
  60. //cout<<ans;
  61. }
  62. else if(l1>l)
  63. {
  64. int f=l1-l;
  65. for(i=0,j=0,k=0;i<l && j<l1 && k<l1;j++,k++)
  66. {
  67. //count++;
  68. if(count!=f)
  69. {ans[k]=s1[j];
  70. count++;
  71. }
  72. else
  73. {int d=(s[i]-'0')+(s1[j]-'0');
  74. int x=d%10;
  75. ans[k]=(char)(x+'0');
  76. i++;
  77. }
  78. //count++;
  79. }
  80. ans[k]='\0';
  81. //cout<<ans;
  82. }
  83. for(i=0;i<k;i++)
  84. {
  85. if(ans[i]=='0')
  86. count1++;
  87. }
  88. if(count1==k)
  89. cout<<0;
  90. else if(temp==1)
  91. {
  92. for(i=k-1;i>=0;i--)
  93. cout<<ans[i];
  94. }
  95. else
  96. cout<<ans;
  97. cout<<endl;
  98. }
  99. return 0;
  100. }
Success #stdin #stdout 0s 15232KB
stdin
Standard input is empty
stdout
Standard output is empty