I’ve solved the FLOW004 with the following code. However I’m unhappy with the solution I’ve come up with. I’m just getting started hence don’t know the tools to solve it in a better way.
#include <bits/stdc++.h>
using namespace std;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int t;
cin>>t;
while(t--)
{
int n, n1, n2;
cin>>n;
n1 = n%10;
n/=10;
while(n)
{
n2 = n%10;
n/=10;
}
cout<<n1+n2<<"\n";
}
}
Perhaps a better method will be to treat the number as a string and then take the first and last element? This will eliminate the need of a loop and make it faster for longer numbers, giving it a constant complexity instead of O(n);
Please help. Thanks!