2
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

AtCoder Beginners SelectionのPHPでの解答例

Posted at

AtCoder Beginners SelectionをPHPで解いてみたのでメモしておきます。時間があったら、解説を加えていきたいです...。

(ないと思いますが)ご質問や、(あると思いますが)ご指摘があれば、コメント欄にてお願いします。

Welcome to AtCoder

PHP
<?php
    fscanf(STDIN, '%d', $a);
    fscanf(STDIN, '%d %d', $b, $c);
    fscanf(STDIN, '%s', $s);
    
    echo $a + $b + $c . " " . $s . "\n";
    
?>

ABC086A - Product

PHP
<?php
    fscanf(STDIN, "%d %d", $a, $b);
    $x = $a * $b;

    echo $x % 2 == 1 ? "Odd\n" : "Even\n"; //三項演算子。下記if文と同義。

    /*if ($x % 2 == 1) {
        echo "Odd";
    } else {
        echo "Even";
    }*/
?>

ABC081A - Placing Marbles

PHP
<?php
    fscanf(STDIN, "%s", $s);
    echo substr_count($s, "1") . "\n";
?>

ABC081B - Shift only

PHP
<?php
    fscanf(STDIN, '%d', $n);
    $array = explode(" ", trim(fgets(STDIN)));
    $n = count($array);
    $count = 0;
    $continue = TRUE;
    
    while ($continue == TRUE) {
        for ($i = 0; $i < $n; $i++) {
            $array[$i] = (int)$array[$i];
            if ($array[$i] % 2 == 1) {
                $continue = FALSE;
                break;
            }
            $array[$i] /= 2;
        }
        $continue == FALSE ? FALSE : $count++;
    } 
    
    echo $count . "\n";

ABC087B - Coins

PHP
<?php
    fscanf(STDIN, "%d", $a);
    fscanf(STDIN, "%d", $b);
    fscanf(STDIN, "%d", $c);
    fscanf(STDIN, "%d", $x);
    $count = 0;
    
    for ($i = 0; $i <= $a; $i++) {
        for ($j = 0; $j <= $b; $j++) {
            for ($k = 0; $k <= $c; $k++) {
                if (500 * $i + 100 * $j + 50 * $k == $x) {
                    $count++;
                }
            } 
        } 
    }
    
    echo $count . "\n";

ABC083B - Some Sums

PHP
<?php
    fscanf(STDIN, "%s %d %d", $n, $a, $b);
    
    for ($i = 1; $i <= $n; $i++) {
        $sum = array_sum(str_split($i, 1));
        if ($sum >= $a && $sum <= $b) {
            $array [] = $i;
        }
    }
   
    echo array_sum($array) . "\n";
  

$array[] = $i;のとこを$count += $i;でカウントアップしていくのもまた善し。

ABC088B - Card Game for Two

PHP
<?php
    fscanf(STDIN, "%d", $n);
    $stdin = explode(" ", trim(fgets(STDIN)));
    foreach ($stdin as $val) {
        $array [] = (int)$val;
    }
    rsort($array);
    $total = array_sum($array);
    $alice = 0;
    
    for ($i = 0; $i < $n; $i++) {
        if ($i % 2 == 0) {
            $alice += $array[$i];
        }
    }
    $bob = $total - $alice;
    echo $alice - $bob . "\n";
    

ABC085B - Kagami Mochi

PHP
<?php
    fscanf(STDIN, "%d", $n);
    for ($i = 0; $i < $n; $i++) {
        fscanf(STDIN, "%d", $array[$i]);
    }
    echo count(array_unique($array)) . "\n";

ABC085C - Otoshidama

PHP
<?php
    fscanf(STDIN, "%d %d", $n, $y);
    
    for ($i = 0; $i <= $n; $i++) {
        for ($j = 0; $i + $j <= $n; $j++) {
            $k = $n - $i - $j;
            if (10000 * $i + 5000 * $j + 1000 * $k === $y) {
                printf("%d %d %d", $i, $j, $k,) . "\n"; exit;
            }
        }
    }
    printf("%d %d %d", -1, -1, -1) . "\n";
    

ABC049C - 白昼夢(Daydream)

PHP
<?php
    fscanf(STDIN, "%s", $s);
    $s = strrev($s);
    while($s !== "") {
        if(substr($s, 0, 5) == "maerd") {
            $s = substr($s, 5);
            continue;
        }
        if(substr($s, 0, 7) == "remaerd") {
            $s = substr($s, 7);
            continue;
        } 
        if(substr($s, 0, 5) == "esare") {
            $s = substr($s, 5);
            continue;
        }
        if(substr($s, 0, 6) == "resare") {
            $s = substr($s, 6);
            continue;
        }
        echo "NO";
        exit();
    }
    echo "YES";
?>

ABC086C - Traveling

PHP
<?php
    fscanf(STDIN, "%d", $n);
    $t = 0;
    $xy = [0, 0];
    
    for ($i = 0; $i < $n; $i++) {
        fscanf(STDIN, "%d %d %d", $nt, $nxy[0], $nxy[1]);
        $dt = $nt - $t;
        $dxy = abs($nxy[0] - $xy[0]) + abs($nxy[1] - $xy[1]);
        if ($dt < $dxy) {
            exit("No\n");
        }
        if (($dt + $dxy) % 2 == 1) {
            exit("No\n");
        }
        $t = $nt;
        $xy = [$nxy[0], $nxy[1]];
    }
    
    echo "Yes\n";
2
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?