0
0

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.

【競プロ】phpでの標準入力まとめ

Last updated at Posted at 2020-05-10

###paizaの標準入力の仕方を一覧にしました。

######1つ、1行のデータ入力
入力例)
a

example.php
<?php
$s = rtrim(fgets(STDIN));
echo $s . "\n";

######3行のデータ入力
入力例)
aaa
bbb
ccc

example2.php
<?php
$s1 = rtrim(fgets(STDIN));
echo $s1 . "\n";
$s2 = rtrim(fgets(STDIN));
echo $s2 . "\n";
$s3 = rtrim(fgets(STDIN));
echo $s3 . "\n";

######N行のデータ入力
入力例)
3 ←N
aaa
bbb
ccc

example3.php
<?php
$n = (int)rtirim(fgets(STDIN));
for ($i = 0; $i < $n; $i++){
$s = rtrim(fgets(STDIN));
echo $s . "\n";
}

($i = 0; $i < $n; $i++)は「同じ処理をn回繰り返す」という意味。
よく出て来るのでこのまま覚えてしまおう。

####1つの行で与えられたデータを分割して処理する方法
######3つのデータの入力の分割処理方法
入力例) aaa bbb ccc
出力例)
aaa
bbb
ccc

example4.php
<?php
$s = rtrim(fgets(STDIN));
$t = explode(" " , $s);
echo $t[0] . "\n";
echo $t[1] . "\n";
echo $t[2] . "\n";

######N個のデータの入力の分割処理方法
入力例)
3←N
aaa bbb ccc
出力例)
aaa
bbb
ccc

example5.php
<?php
$n = (int)rtrim(fgets(STDIN));
$s = rtrim(fgets(STDIN));
$t = explode(" ", $s);
for ($i = 0; $i < $n; $i++) {
    echo $t[$i] . "\n";
}
0
0
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
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?