FLOW004 solution not being accepted

Problem: FLOW004 Problem - CodeChef

The code below runs successfully, gives correct output with custom input. But upon submission, the answer is marked wrong. Can someone help me out with it?

#include
using namespace std;

int main() {
int a, b;
cin>>a;
if (a>=1 && a<=1000){
for (int c=0;c<a;c++){
cin>>b;
if (b>=1 && b<=1000000){
int d=0;
if (b>=10){
d=b%10;
while (b>=10){
b/=10;
}
}
d+=b;
cout<<d<<endl;
}
}
}
return 0;
}

1 Like

Please go through this answer, it should resolve the issue:

1 Like

take all your int as long long int. also why can’t you add the first digit (sum+=a%10)take a single while loop until a>=10 where a=a/10 and then add the last digit. (sum+=a) and print it

Can you look into my code specifically and then point out the error? The person whom you replied to in that answer has a different code. Moreover, as far as I understood the problem, the output for a single digit input should be the single digit itself, not its double. Even when I gave the latter as the output for a sigle digit input, my answer had been marked wrong.

a is the counter for the no. of test cases. b is the particular test case. What you suggested is exactly what I did in my code. The answer is marked wrong.

When the input is a single-digit number then the number itself is the first digit and the last digit, so we are not just doubling it, we are adding it twice.

Now in the above part of your code, due to this condition:

if (b>=10)

value of d in d=b%10; is never assigned for a single-digit input and it remains 0.

Later when you add d+=b; as d value is zero and b value is the input, so d values becomes the same as the input.

So, for a single-digit value, you will print the digit itself instead of its double.

How to resolve this issue??
Remove this condition, that’s it:

if (b>=10)

The problem was with my logic. Single digit had to be indeed added twice. Answer accepted.

The working code:

#include
using namespace std;

int main() {
int a, b;
cin>>a;
if (a>=1 && a<=1000){
for (int c=0;c<a;c++){
cin>>b;
if (b>=1 && b<=1000000){
int d=0;
d=b%10;
if (b>=10){
while (b>=10){
b/=10;
}
}
d+=b;
cout<<d<<endl;
}
}
}
return 0;
}