結果

問題 No.222 引き算と足し算
ユーザー papinianus
提出日時 2016-09-21 12:31:56
言語 PHP
(843.2)
結果
AC  
実行時間 45 ms / 1,000 ms
コード長 582 bytes
コンパイル時間 106 ms
コンパイル使用メモリ 32,020 KB
実行使用メモリ 32,784 KB
最終ジャッジ日時 2024-11-17 10:18:55
合計ジャッジ時間 2,743 ms
ジャッジサーバーID
(参考情報)
judge3 / judge4
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 31
権限があれば一括ダウンロードができます
コンパイルメッセージ
No syntax errors detected in Main.php

ソースコード

diff #

<?php
$str = trim(fgets(STDIN));
$pattern = "/([+-]?[0-9]+)([+-])([+-]?[0-9]+)/";
preg_match_all($pattern, $str, $matches);
$first = getval($matches[1][0]);
$second = getval($matches[3][0]);
if($matches[2][0] == "+")
{
    $ans = $first - $second;
}
else {
    $ans = $first + $second;
}

echo $ans.PHP_EOL;

function getval($str)
{
    $minus = false;
    $index = 0;
    if($str[0] == "+")
    {
        $index = 1;
    }
    if($str[0] == "-")
    {
        $index = 1;
        $minus = true;
    }
    $temp = strval(substr($str, $index));
    return ($minus)?(-$temp):$temp;
}
0