0
0

More than 5 years have passed since last update.

文字列が合うまで、何回配列を動かせばいいか計算するプログラム 【ruby初心者】

Posted at

はじめに

工場などで、向きを調整するプログラム。

実際のコード

#入力の受付
input_line = gets.chomp.split(" ")

#文字を配列になおす
collect = input_line[1].split("") #答えの文字列
testings = input_line[2].split("") #受け取った文字列

#試行回数をカウントする変数
a = 0


testings.each do
    #もしも文字列が一致すれば、終了して試行回数を表示する
    if collect == testings
        puts a
        break
    else
    #文字列が一致しなければ、配列を入れ替える
        t = testings[0]
        testings.shift()
        testings.push(t)
    end
    a += 1
end

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