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 1 year has passed since last update.

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

今回使用する Raku の機能

Raku 入門

変数

3. 変数

my 変数名 = 初期値;

のようにして、変数を宣言できる。
変数のうち、スカラ (1個の値を格納する変数) の変数名は $ ではじめ、配列の変数名は @ ではじめる。

配列の要素には [] を用いてアクセスできる。
配列の最初の要素は0番目である。

my @numbers = 42, 72, 102;
my $chihaya = @numbers[1];

Perl とは異なり、配列の要素にアクセスする際も $ ではなく @ を用いる。

文字列

1.7.3. クォート

文字列は、"" または '' で囲うことで表現できる。

演算子

2. 演算子

今回は、以下の演算子を用いた。

演算子 意味
+ 数値の加算
~ 文字列の結合

入出力

6.1. ターミナルを用いた基本的なI/O

以下のように say を用いることで、「値」を標準出力に出力し、続いて改行文字を標準出力に出力できる。

say 値;

get を用いると、標準入力から1行読み込み、読み込んだ行を表す文字列 (最後に改行文字はつかない) を取得できる。

my $line = get;

get は、式の中で値のように用いることができる。

my $greeting = "Hello, " ~ get;

数値の文字列への変換

class Int | Raku Documentation

数値に対して Str メソッドを使用すると、数値を文字列に変換できる。

my $value = 72;
my $valuestr = $value.Str;

文字列の操作

class Str | Raku Documentation

文字列に対して split(区切り) メソッドを使用すると、文字列を「区切り」(文字列) で区切った列を得ることができる。

my $data = "12 34";
my @parts = $data.split(" ");

文字列に対して Int メソッドを使用すると、その文字列が表す整数に変換できる。

my $data = "72";
my $dataint = $data.Int;

提出コード

my $a = get;
my @bc = get.split(" ");
my $sum = $a.Int + @bc[0].Int + @bc[1].Int;
say $sum.Str ~ " " ~ get;

提出 #54593970 - AtCoder Beginners Selection

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?