Help us understand the problem. What is going on with this article?

AtCoder に登録したら解くべき精選過去問 10 問を PHP で解いてみた

More than 1 year has passed since last update.

はじめに

@drkenさんの記事 AtCoder に登録したら次にやること ~ これだけ解けば十分闘える!過去問精選 10 問 ~ には記事中で取り上げている過去問をいろいろな言語で解いた記事がまとめられています。ですが、PHPで解いた記事は今のところ見あたらなかったのでやってみました。

各問題の詳細は AtCoder Beginners Selection を参照してください。

第 1 問: ABC 086 A - Product (100 点)

<?php
echo array_product(explode(' ', fgets(STDIN))) % 2 ? 'Odd' : 'Even';
?>

第 2 問: ABC 081 A - Placing Marbles (100 点)

<?php
echo substr_count(fgets(STDIN), 1);
?>

第 3 問: ABC 081 B - Shift Only (200 点)

<?php
$n = fgets(STDIN);
$a = explode(' ', fgets(STDIN));
$min = 30;
for ($i = 0; $i < $n; $i++) {
    for ($j = 0; $a[$i] % 2 == 0; $j++) {
        $a[$i] /= 2;
    }
    $min = min($j, $min);
}
echo $min;
?>

第 4 問: ABC 087 B - Coins (200 点)

<?php
$a = fgets(STDIN);
$b = fgets(STDIN);
$c = fgets(STDIN);
$x = fgets(STDIN);
$result = 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) {
                $result++;
            }
        }
    }
}
echo $result;
?>

第 5 問: ABC 083 B - Some Sums (200 点)

<?php
function sum($n) {
    $s = 0;
    while ($n > 0) {
        $s += $n % 10;
        $n /= 10;
    }
    return $s;
}

list($n, $a, $b) = explode(' ', fgets(STDIN));
$total = 0;
for ($i = 1; $i <= $n; $i++) {
    $s = sum($i);
    if ($s >= $a && $s <= $b) {
        $total += $i;
    }
}
echo $total;
?>

第 6 問: ABC 088 B - Card Game for Two (200 点)

<?php
$n = fgets(STDIN);
$a = explode(' ', fgets(STDIN));
rsort($a);
$p = 0;
for ($i = 0; $i < $n; $i++) {
    if ($i % 2 === 0) {
        $p += $a[$i];
    } else {
        $p -= $a[$i];
    }
}
echo $p;
?>

第 7 問: ABC 085 B - Kagami Mochi (200 点)

<?php
$n = fgets(STDIN);
$num = [];
for ($i = 0; $i < $n; $i++) {
    $num[] = trim(fgets(STDIN));
}
echo count(array_unique($num));
?>

第 8 問: ABC 085 C - Otoshidama (300 点)

<?php
list($n, $y) = explode(' ', fgets(STDIN));
$r10000 = -1;
$r5000 = -1;
$r1000 = -1;
for ($a = 0; $a <= $n; $a++) {
    for ($b = 0; $a + $b <= $n; $b++) {
        $c = $n - $a - $b;
        if (10000 * $a + 5000 * $b + 1000 * $c == $y) {
            $r10000 = $a;
            $r5000 = $b;
            $r1000 = $c;
        }
    }
}
printf('%d %d %d', $r10000, $r5000, $r1000);
?>

第 9 問: ABC 049 C - Daydream (300 点)

<?php
$s = strrev(trim(fgets(STDIN)));
$divide = ['dream', 'dreamer', 'erase', 'eraser'];
foreach ($divide as $k => $v) {
    $divide[$k] = strrev($v);
}
$can = true;
for ($i = 0; $i < strlen($s);) {
    $can2 = false;
    foreach ($divide as $v) {
        if (strpos($s, $v, $i) === $i) {
            $can2 = true;
            $i += strlen($v);
        }
    }
    if (! $can2) {
        $can = false;
        break;
    }
}
echo $can ? 'YES' : 'NO';
?>

第 10 問: ABC 086 C - Traveling (300 点)

<?php
$n = fgets(STDIN);
$t = [0];
$x = [0];
$y = [0];
for ($i = 0; $i < $n; $i++) {
    list($t[$i + 1], $x[$i + 1], $y[$i + 1]) = explode(' ', fgets(STDIN));
}
$can = true;
for ($i = 0; $i < $n; $i++) {
    $dt = $t[$i + 1] - $t[$i];
    $dist = abs($x[$i + 1] - $x[$i]) + abs($y[$i + 1] - $y[$i]);
    if ($dt < $dist) {
        $can = false;
    }
    if ($dist % 2 !== $dt % 2) {
        $can = false;
    }
}
echo $can ? 'Yes' : 'No';
?>
rock619
「ご年齢に応じたご経験、スキルが不足されているようです」
https://rock619.com
Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
Comments
No comments
Sign up for free and join this conversation.
If you already have a Qiita account
Why do not you register as a user and use Qiita more conveniently?
You need to log in to use this function. Qiita can be used more conveniently after logging in.
You seem to be reading articles frequently this month. Qiita can be used more conveniently after logging in.
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
ユーザーは見つかりませんでした