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?

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

今回用いる Julia の機能

変数への代入

Variables · The Julia Language

変数名 = 

と書くことで、変数に値を代入できる。

整数の演算

Mathematical Operations and Elementary Functions · The Julia Language

a + b

と書くことで、ab の和を求めることができる。

a - b

と書くことで、a から b を引いた差を求めることができる。

a * b

と書くことで、ab の積を求めることができる。

文字と文字列

Strings · The Julia Language

"hello" のように "" で囲むことで、文字列を表現できる。
文字列内に $ に続けて変数名を書くことで、その変数の値を文字列に埋め込むことができる。

文字列[開始位置:終了位置] のように書くことで、「文字列」の「開始位置」から「終了位置」まで (両端を含む) の部分文字列を得ることが出来る。
位置は 1-origin で表す。
これらの開始位置や終了位置において、最初の文字の位置は begin で、最後の文字の位置は end で表現できる。

'a' のように '' で囲むことで、文字を表現できる。
'b' - 'a' のような文字同士の減算を行うと、文字コードの差が整数で得られる。

findfirst(文字, 文字列) を用いると、「文字列」の中の最初の「文字」の位置を得ることができる。

for 文字を代入する変数 in 文字列
    # 処理内容
end

を用いると、文字列内の各文字について前から順に処理を行うことができる。

関数の定義

Functions · The Julia Language

function 関数名(引数リスト)
    # 処理内容
end

のように書くことで、関数を定義できる。

関数の処理内容の中に

return 

と書くことで、関数から「値」を返して戻ることができる。

入出力

I/O and Network · The Julia Language

println(値) を用いると、標準出力 (など) に「値」を出力し、さらに改行を出力できる。

readline() を用いると、標準入力 (など) から1行読み込み、結果を文字列として得ることができる。
結果の文字列は末尾の改行文字を含まない。(含めるオプションもある)

提出コード

function toint(st)
    res = 0
    for ch in st
        res = res * 10 + (ch - '0')
    end
    return res
end

astr = readline()
bcstr = readline()
s = readline()

spacepos = findfirst(' ', bcstr)
bstr = bcstr[begin:spacepos-1]
cstr = bcstr[spacepos+1:end]

sum = toint(astr) + toint(bstr) + toint(cstr)
println("$sum $s")

提出 #54957587 - 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?