0
0

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] AtCoder過去問 C - Tax Increase

Posted at

はじめに

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

問題はこちらから確認してください↓

C - Tax Increase

まずは入力を受け取ります。
最終的に答えとなる変数ansを用意して、条件を満たす税抜き価格が存在しなかったときに出力する-1を代入しておきます。

a, b = gets.split.map(&:to_i)

ans = -1

続いて消費税は制約からmax100とわかります。つまりmaxの税抜き価格は1000だということです。
1000程度であったらeach文で回せるので1~1000をeachで回してそれぞれの消費税を求めてその答えがaとbと等しいかif文で分岐させます。

ほとんどがfalseを返しますが、trueになったときにansを書き換えてbreakで処理を抜け出します。

最後にansを出力すれば完成です。

a, b = gets.split.map(&:to_i)

ans = -1

(1..1000).each do |i|
  if (i*0.08).floor == a && (i*0.1).floor == b
    ans = i
    break
  end
end

puts ans
0
0
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
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?