×

# Race Track question runtime error

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;


}

1
accept rate: 0%

 toggle preview community wiki:
Preview

By Email:

Markdown Basics

• *italic* or _italic_
• **bold** or __bold__
• image?![alt text](/path/img.jpg "title")
• numbered list: 1. Foo 2. Bar
• to add a line break simply add two spaces to where you would like the new line to be.
• basic HTML tags are also supported
• mathemetical formulas in Latex between \$ symbol

Question tags:

×207
×39
×4

question asked: 05 Sep '17, 17:18

question was seen: 97 times

last updated: 05 Sep '17, 17:18