A - Rock-paper-scissors Editorial by en_translator


If you are new to learning programming and do not know where to start, please try Problem A “Welcome to AtCoder” from practice contest. There you can find a sample code for each language.


In this problem the value to output is dependent on the inputs \(x\) and \(y\). Since there are \(9\) combinations of possible input, one can enumerate every patterns and make a conditional branch with if statement.

Sample code (C)

int main(){
  int x, y;
  scanf("%d %d", &x, &y);
  if(x==0 && y==0) puts("0");
  if(x==0 && y==1) puts("2");
  if(x==0 && y==2) puts("1");
  if(x==1 && y==0) puts("2");
  if(x==1 && y==1) puts("1");
  if(x==1 && y==2) puts("0");
  if(x==2 && y==0) puts("1");
  if(x==2 && y==1) puts("0");
  if(x==2 && y==2) puts("2");
}

One can decrease the number of conditional branches by simplifying into “output \(x\) if \(x\) and \(y\) are equal” and “output the value other than \(x\) and \(y\) if \(x\) and \(y\) are unequal.”

Sample code 2 (C)

int main(){
  int x, y;
  scanf("%d %d", &x, &y);
  if(x==y) printf("%d", x);
  else printf("%d", 3-x-y);
}

posted:
last update: