< Back to forum

it is giving wrong answer

https://www.codechef.com/problems/FBMT

  1. #include <stdlib.h>

  2. #include <string.h>

  3. #include <stdlib.h>

  4. int main() {

  5. int t, len;

  6. int c1,c2,j;

  7. char s1[21];

  8. char s2[21];

  9. char temp[21];

  10. scanf("%d", &t);

  11. while(!t)

  12. {

  13. c1=1;

  14. c2=0;

  15. scanf("%d",&len);

  16. scanf("%s",&s1);

  17. for(j=1;j<len;j++)

  18. {

  19. scanf("%s",&temp);

  20. if((strcmp(s1,temp)==0))

  21. c1++;

  22. else

  23. {

  24. strcpy(s2,temp);

  25. c2++;

  26. }

  27.  
  28.  
  29. }

  30.  
  31.  
  32.  
  33. if(c1 > c2)

  34. printf("%s\n", s1);

  35. else if(c2 > c1)

  36. printf("%s\n", s2);

  37. else

  38. printf("Draw\n");

  39. t--;

  40. }

  41. return 0;

  42. }

  43.  

Asked by: Ankan_Ghosh on April 7, 2019, 6:34 p.m. Last updated on April 7, 2019, 6:34 p.m.


Enter your answer details below:


Enter your comment details below:




1 Answer(s)

avatar

Errors in your code:

1. while(!t) : if t>0 , while loop is never executed as while(!t) evaluates to false. Use while(t).

2. scan("%s",&s1): You don't need to use '&' while taking string input.   (Same for temp)

Note: Instead of copying the value of temp in s2 again and again, try to do that once.

Ruchi_Singh last updated on April 7, 2019, 6:34 p.m. 0    Reply    Upvote   

Instruction to write good question
  1. 1. Write a title that summarizes the specific problem
  2. 2. Pretend you're talking to a busy colleague
  3. 3. Spelling, grammar and punctuation are important!

Bad: C# Math Confusion
Good: Why does using float instead of int give me different results when all of my inputs are integers?
Bad: [php] session doubt
Good: How can I redirect users to different pages based on session data in PHP?
Bad: android if else problems
Good: Why does str == "value" evaluate to false when str is set to "value"?

Refer to Stack Overflow guide on asking a good question.