fork download
  1. <?php
  2.  
  3. class Klasemen {
  4. private $poin = [];
  5.  
  6. public function __construct($daftarKlub) {
  7. foreach ($daftarKlub as $klub) {
  8. $this->poin[$klub] = 0;
  9. }
  10. }
  11.  
  12. public function catatPermainan($klubKandang, $klubTandang, $skor) {
  13. list($golKandang, $golTandang) = explode(':', $skor);
  14. $golKandang = (int)$golKandang;
  15. $golTandang = (int)$golTandang;
  16.  
  17. if ($golKandang > $golTandang) {
  18. $this->poin[$klubKandang] += 3;
  19. } elseif ($golKandang < $golTandang) {
  20. $this->poin[$klubTandang] += 3;
  21. } else {
  22. $this->poin[$klubKandang] += 1;
  23. $this->poin[$klubTandang] += 1;
  24. }
  25. }
  26.  
  27. public function cetakKlasemen() {
  28. $klasemen = $this->poin;
  29. arsort($klasemen);
  30. return $klasemen;
  31. }
  32.  
  33. public function ambilPeringkat($nomorPeringkat) {
  34. $klasemen = $this->cetakKlasemen();
  35. $klub = array_keys($klasemen);
  36. if ($nomorPeringkat >= 1 && $nomorPeringkat <= count($klub)) {
  37. return $klub[$nomorPeringkat - 1];
  38. }
  39. return null;
  40. }
  41. }
  42.  
  43. $klubList = explode(',', trim(fgets(STDIN)));
  44. $klasemen = new Klasemen($klubList);
  45.  
  46. while ($line = fgets(STDIN)) {
  47. $line = trim($line);
  48. if ($line === '') continue;
  49. // Format: KlubA-KlubB Skor
  50. list($tim, $skor) = explode(' ', $line);
  51. list($kandang, $tandang) = explode('-', $tim);
  52. $klasemen->catatPermainan($kandang, $tandang, $skor);
  53. }
  54.  
  55. print_r($klasemen->cetakKlasemen());
  56. echo "Peringkat ke-2: " . $klasemen->ambilPeringkat(2) . "\n";
  57.  
Success #stdin #stdout #stderr 0.03s 26172KB
stdin
Liverpool, Chelsea, Arsenal
Arsenal-Liverpool 2:2
Arsenal-Chelsea 1:9
Chelsea-Arsenal 9:6
Chelsea-Liverpool 9:4
Liverpool-Arsenal 2:2
Liverpool-Chelsea 0:1
stdout
Array
(
    [Chelsea] => 12
    [Liverpool] => 2
    [Arsenal] => 2
    [ Chelsea] => 0
    [ Arsenal] => 0
)
Peringkat ke-2: Liverpool
stderr
PHP Notice:  Undefined index: Arsenal in /home/uEdGG8/prog.php on line 22
PHP Notice:  Undefined index: Chelsea in /home/uEdGG8/prog.php on line 20