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.

【At Corder】【初心者】Welcome to AtCoder をRuby で解いてみた

Last updated at Posted at 2021-03-29

はじめに

AtCoder に登録したら次にやること ~ これだけ解けば十分闘える!過去問精選 10 問 ~ - Qiita

こちらの記事を参考に初心者がAt corderに挑戦します。
目的としては、就職活動でのコーディングテスト対策です。
毎日1問を目標としてコツコツやってきます:thinking:

わからないことは調べる精神です。ちょっとでもわからないなぁ、と思ったことは調べて解説と参考文献を載せますので、同じ内容でわからない人がいれば参考にししてください:baby:

問題 Welcome to AtCoder

整数 a,b,cと、文字列 s が与えられます。 a+b+c の計算結果と、文字列 s を並べて表示しなさい。

入力は

a
b c
s

出力は
a+b+cとsを空白区切りで1行に出力

入力例

1
2 3
test

出力例

6 test

回答

a = gets.to_i  
b,c=gets.chomp.split(" ").map(&:to_i);
s = gets.chomp

print("#{a+b+c} #{s}\n")

解説

get メソッドは入力を文字列で受ける。改行も含める。

.chompは改行をはずす

.to_iは数値オブジェクトに変換

二行目 入力を配列に格納(to_iなので数値として保存)

split(sep = $;, limit = 0)は第 1 引数 sep で指定されたセパレータによって文字列を limit 個まで分割し、結果を文字列の配列で返します。ブロックを指定すると、配列を返す代わりに分割した文字列でブロックを呼び出します。(今回はlimitはない)

感想

なにを使って入力を受けるのか、データ型はなにか、など基本的なことを押さえていないと入力を受け付けることすらできない。
コツコツ理解していこう:muscle:

参考文献

0
0
1

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?