1
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?

Qiita Engineer Festa 2024(キータ・エンジニア・フェスタ 2024) - Qiita
において、約1ヶ月で38記事という大量の記事の投稿を要求されることがわかった。
そこで、あまりコストをかけずに記事数を稼ぐ方法を考えた結果、「Welcome to AtCoder を様々な言語で解く」ことを思いついた。
単に解くだけでなく、使用する言語仕様の解説を入れれば、記事として一応成立するだろう。

Welcome to AtCoder

PracticeA - Welcome to AtCoder

Welcome to AtCoder では、以下の形式で整数 $a$, $b$, $c$ および文字列 $s$ が入力として与えられる。

a
b c
s

この入力をもとに、与えられた整数の和 $sum = a + b + c$ および文字列 $s$ を、以下の形式で出力することが求められる。

sum s

今回用いる AWK の仕様

awkの使いかた #Linux - Qiita
とほほのAWK入門 - とほほのWWW入門

入力に対し、1行ごとに処理を行う。

処理を行う条件 {
  処理内容
}

の形式で、処理を行う条件と処理内容を記述する。
処理を行う条件を指定した場合は、この条件に合う行についてこの処理を行う。
処理を行う条件を空にした場合は、すべての行についてこの処理を行う。

組み込み変数 NR を用いると、対象の行の行番号を取得できる。
最初の行の行番号は 1 である。

処理内容では、$0 で処理対象の行全体を、$n ($1$2 など) でその行の n 番目のフィールド (空白文字で区切られた部分) を参照できる。

処理では変数を用いることができる。
変数の宣言は不要で、いきなり用いてよい。
変数への代入は = を用いて行うことができる。

print を用いると、データを出力できる。
複数のデータを並べて書くと、すべて結合して出力できる。

提出コード

1行目・2行目に対して、それぞれ与えられる数値を読み込み、和を求める。
3行目に対して、求めた和・空白・3行目の内容を続けて出力する。

NR == 1 {
  sum = $1
}

NR == 2 {
  sum = sum + $1 + $2
}

NR == 3 {
  print sum " " $0
}

提出 #54439508 - AtCoder Beginners Selection

1
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
1
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?