Coding
  • Welcome
  • Baisc Knowledge
    • Vim
    • C
      • Library
    • Java
      • Setup
      • Java Basic
  • Kattis
    • Easy
      • A Second Opinion
      • A Shortcut to What?
      • A Stack of Gold
      • ACM Contest Scoring
      • ASCII kassi
      • Aaah!
      • Add Two Numbers
      • Adding Trouble
      • Afjörmun
      • Airfare Grants
      • Above Average
      • Akcija
      • Alphabet Spam
      • Amerískur vinnustaður
      • Anti-Palindrome
      • Apaxiaaaaaaaaaaaans!
      • Arithmetic Functions
      • Arm Coordination
      • Arrangement
      • Attendance
      • Autori
      • Average Character
      • Avion
      • Baby Bites
      • Babylonian Numbers
      • ABC
      • Aldur
      • Backspace
      • Bannorð
      • Barcelona
      • Basketball One-on-One
      • Batter Up
      • Beavergnaw
      • Bela
      • BergMál
      • Bergur*
      • Akureyri*
      • Best Compromise
      • Best Relay Team*
      • Besta gjöfin
      • Betting
      • Bijele
      • Bilað Lyklaborð
      • Bitte ein Bit
      • Blandað Best
      • Blaðra
      • Blaðra2
      • Bluetooth*
      • Booking a Room
      • Bottle Opening
      • Bounding Robots
      • Breaking Branches*
      • Bracket Matching*
      • Broken Swords
      • Building Pyramids
      • Bus
      • Bus Assignment
      • CPR Number
      • Call for Problems
      • Canadians, eh?
      • Candy Store
      • Cetiri
      • Cetvrta
      • Champernowne Verification
      • Chanukah Challenge
      • Chardonnay
      • Chocolate Division*
      • Chugging
      • Cinema Crowds 2
      • Class Field Trip
      • ASCII Kassi 2
      • Coffee Cup Combo
      • Cold-puter Science
      • Composed Rhythms
      • Cookies
      • Cooking Water
      • Cornhusker
      • Cosmic Path Optimization
      • Count the Vowels
Powered by GitBook
On this page
  • Question
  • Solution
  • Idea
  • Code
Edit on GitHub
  1. Kattis
  2. Easy

Basketball One-on-One

PreviousBarcelonaNextBatter Up

Last updated 6 months ago

Question

Solution

Idea

To solve this question trivially, you can

  1. Iterate through the input and record the score for A and score for B

  2. And after exiting the loop, check whose score is higher

I haven't tried this solution so I am not sure whether it will work or not. But this solution is not elegant! And the more elegant or faster way is to add the following steps to our original solution:

  1. If score_a >= 11 && score_a - score_b >= 2, that means A wins, print the result, return.

  2. If score_b >= 11 && score_b - score_a >= 2, that means B wins, print the result, return.

Note that steps 3 and 4 are not in if/else relation, that means you need to use two if to judge.

Code

https://open.kattis.com/problems/basketballoneononeopen.kattis.com
https://github.com/mendax1234/Coding-Problems/blob/main/kattis/basketballoneonone/basketballoneonone.c
#include <stdio.h>
#include <string.h>
#include <stdbool.h>

#define MAX_LEN 200

int main()
{
  char line[MAX_LEN];

  if (fgets(line, MAX_LEN, stdin))
  {
    line[strcspn(line, "\n")] = 0;
  }
  
  long score1 = 0;
  long score2 = 0;
  for (long i = 0; line[i] != 0; i += 2)
  {
    if (line[i] == 'A')
    {
      score1 += (long)line[i+1] - (long)'0';
    }
    else
    {
      score2 += (long)line[i+1] - (long)'0';
    }
    if (score1 >= 11 && score1 - score2 >= 2)
    {
      printf("A\n");
      return 0;
    }
    if (score2 >= 11 && score2 - score1 >= 2)
    {
      printf("B\n");
      return 0;
    }
  }
}