#include <iostream>
#include<stdio.h>
#include<string.h>
using namespace std;

int main()
{
   // cout << "Hello world!" << endl;
    int t,i,j,a,flag;
    char str[1000001];
    cin>>t;
    while(t--)
    {
        a=1;
        scanf("%s",str);
        for(i=0;i<=strlen(str);i++)
        {flag=0;
            if(!isdigit(str[i]))
            {flag=0;
                for(j=i-1;isdigit(str[j]);j--)
                {
                    if(str[j]!='0')
                    {
                        flag++;
                        a*=str[j]-'0';
                        //cout<<"flag"<<flag<<" "<<a<<"A"<<endl;
                        break;
                    }
                }
                 if(flag==0)
                {
                    a=0;break;
                }
            }


                    while(a>9)
                    {
                        if((a%10)!=0)
                        {
                            a=a%10;
                            //cout<<"a"<<a<<endl;
                        }
                        else
                        {
                            a=a/10;
                        }

                    }


        }
        if(a)
        printf("%d\n",a);
        else
        printf("Robot hanged.\n");
    }
    return 0;
}

This is the code for the following problem link.
I am getting a wrong answer despite my code working for all the test cases.
Can anyone tell me for which test case my code is not working.

A robot named as Maestro, works on right most digit of number. For eg. 123456 is a number. Maestro would work with 6. But now, if number is 12345600. It will work with 6.
Input Specification

Input a variable t which is the number of test cases. Then input a string having alternate integers and multiplication characters. Your job is to calculate the last non zero digit in that mathematical expression. Number can be as large as 10^19. 0 < t<100
Output Specification

Output consists of a single number which is the last non zero digit of the value of the expression. If the expression evaluates to be zero, output should be “Robot hanged.”
Example

Sample Input:
2
2X3X7X5
2X0X4X25


Sample Output:
1
Robot hanged.

There are many things that are wrong. I am not figuring all of them out for you but will give some help.

(a) Consider the test case: 15X2 Expected output 3 (because 30 goes to 3)/ Actual output 1.

So what went wrong. It is in your multiplication. You don't keep any account of multiple digit number e.g. you do 1x5x2 and get 10 which is wrong.

(b) The specification says that you will need to deal with numbers up to 1e19. That doesn't fit in an integer [on most computers].

Overall your algorithm needs a re-think. Sorry.

You have to figure out a way that you can truncate the multiplication number or ignore the parts that don't matter. That can be done (for example -- there are many ways), by noting that 10 has prime factors 2 and 5.

-- Parse a result with zero in it as special
-- Remove the factors of 10 (e.g. a 2 and a 5 prime factor). You are left with a key result:
if you have a remaining factor 5, your result is 5
if not multiply all the last digits together and keeping only the last digit at each stage

The above is definitely not optimal!! It is an example that can be shown to work with very very large input.

Edited 4 Years Ago by StuXYZ: n/a

This question has already been answered. Start a new discussion instead.