LoginSignup
0
0

More than 3 years have passed since last update.

配列において、前後の数字の差をとる【ruby初心者】

Last updated at Posted at 2019-06-24

はじめに

配列において、前後の数字の差をとる、プログラムを作ってみる

方法①ごりごりやる

#入力データ
input_line = readlines.map(&:to_i)

#(配列数-1)回実施する
count = input_line.count-1

#差をとる
count.times do |timeCount|
    puts  input_line[timeCount+1] - input_line[timeCount]
end

方法②each_consをつかう

input_line = readlines.map &:to_i

#前後の配列を作る
before_after = input_line.each_cons(2).to_a

before_after.each do |ba|
    puts ba[1] - ba[0]
end
0
0
2

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