i got WA in the sum of first and last digit problem, checked the test cases so many times yet i could not find the error. Please help.
Basically in your code, you aren’t saving much of complexity by checking for single digit numbers and also missing the condition for n == 10, anyways, straight forward approach would also do.
My solution got AC, so there is no fault in the test cases. Please re-check your approach.
Scanner sc = new Scanner(System.in);
if (sc.hasNext()) {
int t = sc.nextInt();
while (t-- > 0) {
int n = sc.nextInt();
String s = String.valueOf(n);
int a = Integer.parseInt(s.substring(0, 1));
int b = Integer.parseInt(s.substring(s.length() - 1));
System.out.println(a + b);
}
}
sc.close();
Oneliner approach could be,
System.out.println(n % 10 + Integer.parseInt(Integer.toString(n).substring(0, 1)));
Can’t see your code.
Thank you for helping me but i am still getting WA. Tried your code as well, got a WA.
please throw some light.
.
.
.
/* package codechef; // don’t place package name! */
import java.util.;
import java.lang.;
import java.io.*;
/* Name of the class has to be “Main” only if the class is public. */
class Codechef
{
public static void main (String[] args) throws java.lang.Exception
{
// your code goes hb
Scanner sc=new Scanner(System.in);
int t=sc.nextInt();
while(t–>0)
{
int n=sc.nextInt();
int l=n%10;
if(n>=10)
{
String s=Integer.toString(n);
String s1=s.substring(0,1);
int f=Integer.parseInt(s1);
int sum=l+f;
System.out.println(sum);
}
else
System.out.println(l);
}
}
}
How come my answer got AC and when the same answer you submitted got WA
there is surely some test case which i am failing to recognise …will check once again
The expected result for the test input:
1
6
is 12
, not 6
.