Skip to content

Instantly share code, notes, and snippets.

@flymio
Created September 27, 2018 09:20
Show Gist options
  • Save flymio/01397aab870cd09f0edbe1f1332de575 to your computer and use it in GitHub Desktop.
Save flymio/01397aab870cd09f0edbe1f1332de575 to your computer and use it in GitHub Desktop.
my new knowledges
<?php
// Complete the diagonalDifference function below.
function diagonalDifference($arr) {
$count = count($arr);
$d1=0; $d2=0;
for($i=0;$i<$count;$i++){
$d1+=$arr[$i][$i];
$d2+=$arr[$i][$count-$i-1];
}
return abs($d2-$d1);
}
$fptr = fopen(getenv("OUTPUT_PATH"), "w");
$stdin = fopen("php://stdin", "r");
fscanf($stdin, "%d\n", $n);
$arr = [];
for ($i = 0; $i < $n; $i++) {
fscanf($stdin, "%[^\n]", $arr_temp);
$arr[] = array_map('intval', preg_split('/ /', $arr_temp, -1, PREG_SPLIT_NO_EMPTY));
}
$result = diagonalDifference($arr);
echo $result;
fwrite($fptr, $result . "\n");
fclose($stdin);
fclose($fptr);
1. забрать весь файл и по строчкам разложить: $lines = explode(PHP_EOL, file_get_contents('php://stdin'));
2. $n = array_shift($lines);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment