LoginSignup
0
0

More than 3 years have passed since last update.

Ruby カロリー計算 アウトプット

Posted at

自分用のアウトプットです。

食べた食品のカロリー計算をするプログラムをしました。

M = 食品の品数
N = 人数
food = 1gあたりのカロリー
eat = 何g食べたか
carolie = トータルカロリー

qiita.rb
M,N = gets.chomp.split(" ").map{|i| i.to_i}

food = []
i = 0
M.times do 
  i = (gets.to_f / 100)
  food.push(i)
end


eat = []

N.times do
  i = gets.chomp.split(" ").map{|i| i.to_i}
  eat.push(i)
end

i = 0
f = 0
total = []
count = N
count1 = M - 1
calorie = 0

while count > 0 do
  for s in 0..count1 do
    calorie += (food[i] * eat[f][s]).floor
    i = i + 1
  end
  total.push(calorie)
  calorie = 0
  i = 0
  f = f + 1
  count = count - 1
end


puts total

あまりfor文使い慣れていないのでその点今回使えたのはよかったですかね。
おそらく今回2次元配列を使えばもっと凝縮できた感がありましたが、うまくいきませんでした。。。

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