Why is my perfect code is NOT correct ?

This is very weird for me.

The problem code is : FLOW004
problem link is : FLOW004 Problem - CodeChef

This code :

#include
using namespace std;

int main() {
// your code goes here
int t;
cin>>t;
while(t–)
{
int n,l,f;
cin>>n;
l=n%10;
while(n != 0)
{
f = n%10;
n=n/10;
}

	cout<<f+l<<endl; 
}
return 0;

}

Works perfectly and gets accepted on the submission.

But below code :

#include
using namespace std;

int main() {
// your code goes here
int t ;
cin >> t;
while(t–){
int n;
cin >> n;
int last_digit = n%10;
while(n>10){
n = n/10;
}
cout << n + last_digit << endl;
}
return 0;
}

is NOT accepted.

I means both programs give the same result so why does one gets accepted and other not ?

The WA version gives the wrong answer for the following test input:

1
10
1 Like

Thanks, I fixed the code by making ,

while(n>=10) { … }

Turns out that my perfect code was not useful :sweat_smile:

1 Like