LoginSignup
0
0

More than 1 year has passed since last update.

[Ruby] AtCoder 過去問 B - 1%

Posted at

はじめに

AtCoderの過去問B問題を解いてみました。
よろしくお願いします。

問題は下記リンクから確認してください。

B - 1%

まず、入力受け取りをします。
今回は一つだけなのでgets.to_iでいけます。
この入力値=目標金額になるのでgoal_balanceとでもしておきます。

goal_balance = gets.to_i

高橋くんはもともと100円を銀行に預けているので変数に入れておきます。
また、このあとwhile文で使う繰り返した回数を数えてくれる変数を用意して0を代入しておきます。

このカウント=年数です。

goal_balance = gets.to_i

bank_balance = 100
year = 0

これで使う変数の準備ができました。
あとはwhile文で処理して、yearを出力すればオッケーです。

goal_balance = gets.to_i

bank_balance = 100
year = 0

while goal_balance > bank_balance
  bank_balance += bank_balance / 100
  year += 1
end

puts year

貯金額が目標金額未満だったら処理を繰り返すように条件式を記述し、処理としては、1%足していくので単純に100で割れば現在の貯金額の1%が割り出せます。
それを貯金額に足していく処理になります。

最後に年数(繰り返した回数)を出力したら完了です。

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