You are given the initial health and tracks for 2 contestants - A and B The tracks are denoted as strings of characters ‘_’,’+’,’-’,’*’. The ending of the track is denoted using ‘|’. The contestants loose or gain health each step as follows:
'_' = -1
'*' = -2
'-' = -1
'+' = +1
If someone’s health drops to zero or less s/he cannot run anymore. The one who stays longest on the track wins (The one who completes larger number of steps). If its a tie, A wins. You have to print the name of winner (A or B) Note: Assume health to be int. Also keep in mind that you do not know the length of the track beforehand. Input: A’s health (integer) A’s track (string) B’s health (integer) B’s track (string) Example: Input: 10 —*±—| 9 -±*±—| Output: B
I wrote this program but it is showing time limit exceeded.
#include<stdio.h>
int main()
{
int ha,i=0,j=0,hb;
scanf("%d",&ha);
while(ha>0)
{
char c;
scanf("%c",&c);
if(c=='-'||c=='_')
{
ha=ha-1;
}
else if(c=='*')
{
ha=ha-2;
}
else if(c=='+')
{
ha=ha+1;
}
i++;
}
scanf("%d",&hb);
while(hb>0)
{
char c;
scanf("%c",&c);
if(c=='-'||c=='_')
{
hb=hb-1;
}
else if(c=='*')
{
hb=hb-2;
}
else if(c=='+')
{
hb=hb+1;
}
j++;
}
if (i>=j)
{
printf("A");
}
else{
printf("B");
}
return 0;
}