Stone-paper-scissor-lizard-Spock in Uri Online Judge ex. 1873, but he does not accept

Asked

Viewed 645 times

0

My attempt:

#include <stdio.h>
#include <string.h>

int main() {
  char rajesh[15], sheldon[15];
  char pedra[] = "pedra", papel[] = "papel", tesoura[] = "tesoura",
  lagarto[] = "lagarto", spock[] = "spock";
  int c, i, comp;

  printf("Digite a quantidade de casos de teste: \n");
  scanf("%d", &c);
  for(i = 0; i < c; i++) {
    printf("Digite uma frase: \n");
    scanf(" %s %s", rajesh, sheldon);
    comp = strcmp(rajesh, sheldon);
    if(comp == 0) {
      printf("empate\n");
    }
    if(comp != 0) {
      if((strcmp(rajesh, tesoura) == 0) && ((strcmp(sheldon, papel) == 0) ||
        (strcmp(sheldon, lagarto) == 0))){
        printf("radesh\n");
      } else if((strcmp(sheldon, tesoura) == 0) && ((strcmp(rajesh, papel) == 0) ||
        (strcmp(sheldon, lagarto) == 0))){
        printf("sheldon\n");
      }

      if((strcmp(rajesh, papel) == 0) && ((strcmp(sheldon, pedra) == 0) ||
        (strcmp(sheldon, spock) == 0))){
        printf("radesh\n");
      } else if((strcmp(sheldon, papel) == 0) && ((strcmp(rajesh, pedra) == 0) ||
        (strcmp(rajesh, spock) == 0))){
        printf("sheldon\n");
      }

      if((strcmp(rajesh, pedra) == 0) && ((strcmp(sheldon, lagarto) == 0) ||
        (strcmp(sheldon, tesoura) == 0))){
        printf("radesh\n");
      } else if((strcmp(sheldon, pedra) == 0) && ((strcmp(rajesh, lagarto) == 0) ||
        (strcmp(rajesh, tesoura) == 0))){
        printf("sheldon\n");
      }

      if((strcmp(rajesh, lagarto) == 0) && ((strcmp(sheldon, spock) == 0) ||
        (strcmp(sheldon, papel) == 0))){
        printf("radesh\n");
      } else if((strcmp(sheldon, lagarto) == 0) && ((strcmp(rajesh, spock) == 0) ||
        (strcmp(rajesh, papel) == 0))){
        printf("sheldon\n");
      }

      if((strcmp(rajesh, spock) == 0) && ((strcmp(sheldon, tesoura) == 0) ||
        (strcmp(sheldon, pedra) == 0))){
        printf("radesh\n");
      } else if((strcmp(sheldon, spock) == 0) && ((strcmp(rajesh, tesoura) == 0) ||
        (strcmp(rajesh, pedra) == 0))){
        printf("sheldon\n");
      }
    }
  }

  return 0;
}

Link to the statement: https://www.urionlinejudge.com.br/judge/pt/problems/view/1873

  • Puts the type of error it returns. If you’re going to wait for someone to log in or create an account just to test your code, figure out the answer and then try to help you, I think the answer will take

  • It returns this error: Wrong Answer (40%).

1 answer

1


Look at this:

    if(comp == 0) {
      printf("empate\n");
    }
    if(comp != 0) {

Well, if the first if enter, automatically the second does not enter and vice versa. Therefore, it is better to use a else here instead of another if.

Then these piles of ifwere supposed to be else if:

      if((strcmp(rajesh, tesoura) == 0) && ((strcmp(sheldon, papel) == 0) ||
        (strcmp(sheldon, lagarto) == 0))){
        printf("radesh\n");
      } else if((strcmp(sheldon, tesoura) == 0) && ((strcmp(rajesh, papel) == 0) ||
        (strcmp(sheldon, lagarto) == 0))){
        printf("sheldon\n");
      } else if((strcmp(rajesh, papel) == 0) && ((strcmp(sheldon, pedra) == 0) ||
        (strcmp(sheldon, spock) == 0))){
        printf("radesh\n");
      } else if((strcmp(sheldon, papel) == 0) && ((strcmp(rajesh, pedra) == 0) ||
        (strcmp(rajesh, spock) == 0))){
        printf("sheldon\n");
      }

The reason is that it should only fall into one printf. The way you did, without forcing everything to be else if, several printfs are fired.

The code goes like this:

    if (comp == 0) {
        printf("empate\n");
    } else if ((strcmp(rajesh, tesoura) == 0) && ((strcmp(sheldon, papel) == 0) || (strcmp(sheldon, lagarto) == 0))) {
        printf("radesh\n");
    } else if ((strcmp(sheldon, tesoura) == 0) && ((strcmp(rajesh, papel) == 0) || (strcmp(sheldon, lagarto) == 0))) {
        printf("sheldon\n");
    } else if ((strcmp(rajesh, papel) == 0) && ((strcmp(sheldon, pedra) == 0) || (strcmp(sheldon, spock) == 0))) {
        printf("radesh\n");
    } else if ((strcmp(sheldon, papel) == 0) && ((strcmp(rajesh, pedra) == 0) || (strcmp(rajesh, spock) == 0))) {
        printf("sheldon\n");
    } else if ((strcmp(rajesh, pedra) == 0) && ((strcmp(sheldon, lagarto) == 0) || (strcmp(sheldon, tesoura) == 0))) {
        printf("radesh\n");
    } else if ((strcmp(sheldon, pedra) == 0) && ((strcmp(rajesh, lagarto) == 0) || (strcmp(rajesh, tesoura) == 0))) {
        printf("sheldon\n");
    } else if ((strcmp(rajesh, lagarto) == 0) && ((strcmp(sheldon, spock) == 0) || (strcmp(sheldon, papel) == 0))) {
        printf("radesh\n");
    } else if ((strcmp(sheldon, lagarto) == 0) && ((strcmp(rajesh, spock) == 0) || (strcmp(rajesh, papel) == 0))) {
        printf("sheldon\n");
    } else if ((strcmp(rajesh, spock) == 0) && ((strcmp(sheldon, tesoura) == 0) || (strcmp(sheldon, pedra) == 0))) {
        printf("radesh\n");
    } else if ((strcmp(sheldon, spock) == 0) && ((strcmp(rajesh, tesoura) == 0) || (strcmp(rajesh, pedra) == 0))) {
        printf("sheldon\n");
    }

Then observe these two lines:

  printf("Digite a quantidade de casos de teste: \n");
    printf("Digite uma frase: \n");

The problems of the URI are extremely rigorous and inflexible as to the input and output format, even a more or less blank space makes him say that his program is wrong. These two above messages will appear in the output and make the URI consider your solution invalid. Remove these messages.

Incidentally, the URI format also does not tolerate any typos or spelling errors on the output. The player’s name is rajesh and not radesh.

And we have this:

    scanf(" %s %s", rajesh, sheldon);

Are you sure it wasn’t meant to be "%s %s" instead of " %s %s"? I think this blank space at first might get in your way.

There’s a lot more things I could suggest to that lot of ifs get a legal structure and not this hideous and repetitive thing. However, then I would have to practically rewrite your entire program.

  • Forgive the mess in the code, I’m still learning to code. I made the changes in the code but Uri still do not accept. Still, I wanted to thank you for the tips, and thank you for your attention.

  • Problem solved personal. But once, thanks for helping Victor.

  • 1

    @Andrémoreira If this answer solved your problem and left no doubt, mark it as correct/accepted by clicking on the " " that is next to it, which also marks your question as solved. If you still have any questions or would like further clarification, feel free to comment.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.