2
3

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.

Ruby  簡易株の自動売買システム

Posted at

初学者のアウトプットです。

株の売買システムを作ってみました。

下記コード

n_1 >= trade 株買取
n_2 <= trade 株全て売却

profit ・・・最終的な利益
配列の最後の値になると全て売却することにしています。

qiita.rb
n,n_1,n_2 = gets.chomp.split(" ").map{|i| i.to_i}
i = 0
profit = 0
stock = 0
trade = []

n.times do 
  i = gets.to_i
  trade.push(i)
end

a = 0

while n > 0 do
  if n == 1
    profit +=  trade[a] * stock
  elsif n_1 >= trade[a]
      profit -= trade[a]
      stock += 1
  elsif n_2 <= trade[a]
      profit += trade[a] * stock
      stock = 0
  end
  a = a + 1
  n = n - 1
end

puts profit

やっぱり一番気になるのは最初に変数と配列の宣言をやりまくってしまうこと
いつみてもダサすぎる。。。。

個人的にロジックは結構うまく(あくまで私レベルの中では)組めたと思いました。

こうすると良いよーってあったらどんどんコメントください!

2
3
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
2
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?