problem 1 peer c++


I have been trying to solve this problem and I always got TLE.
#include<iostream>
using namespace std;

int main()  { 
int number, number2, reverse = 0, reverse2 = 0, reverse3 = 0, testCases, sum; 
cin >> testCases; 
for (int x = 1; x <= testCases; x++) 
{ 
    cin >> number; 
 
for( ; number!= 0 ; ) 
   { 
      reverse = reverse * 10; 
      reverse = reverse + number%10; 
      number = number/10; 
   } 
    cin >> number2; 
for( ; number2!= 0 ; ) 
   { 
      reverse2 = reverse2 * 10; 
      reverse2 = reverse2 + number%10; 
      number = number/10; 
   } 
 
   sum = reverse + reverse2; 
 
for( ; sum!= 0 ; ) 
    { 
      reverse3 = reverse3 * 10; 
      reverse3 = reverse3 + number%10; 
      number = number/10; 
    } 
    cout << reverse3 << endl; 
} 
return 0; 
} 

And yes, I am a novice programmer, any idea how to shorten the execution time? I have not yet learned advance programming concepts like scanf so I would appreciate helping me with improving my algorithm. Thanks

  1. Your algorithm seems fine as per the time limit, but you haven’t handled reversal of negative numbers. Also, note that the time limit is 50 sec which is enough for your method to work.

  2. scanf( ) is just a function in GCC that does the same job as cin if you see from a high-level perspective, ignoring inner working of both. You can see codes of other users and learn how they use scanf( ). It is not so difficult to comprehend.

1 Like