LoginSignup
1
1

More than 3 years have passed since last update.

ABC087B - Coins

Last updated at Posted at 2019-11-25

AtCoderやれ!!!!!って言われたので
問題はこれ↓
https://atcoder.jp/contests/abs/tasks/abc087_b

問題

スクリーンショット 2019-11-25 18.06.05.png

1回目

まずは自力で

回答

A = gets.to_i
B = gets.to_i
C = gets.to_i
X = gets.to_i

count = 0
for a in 0..A do
  for b in 0..B do
    for c in 0..C do
      if a * 500 + b * 100 + c * 50 == X then
        count += 1
      end
    end
  end
end

puts count

結果

スクリーンショット 2019-11-25 18.07.49.png

2回目

他の人の回答を見て少し直す

回答

A = gets.to_i
B = gets.to_i
C = gets.to_i
X = gets.to_i

count = 0
for a in 0..A do
  for b in 0..B do
    x = X - a * 500 - b * 100
    if x >= 0 && x % 50 == 0
      for c in 0..C do
        if c * 50 == x then
          count += 1
          break
        end
      end
    end
  end
end

puts count

結果

スクリーンショット 2019-11-25 18.09.42.png

まとめ

処理時間が 24 → 14 になった
思ったより楽しい

追記-3回目-

回答

A = gets.to_i
B = gets.to_i
C = gets.to_i
X = gets.to_i

sum = 0
count = 0
A.step(0, -1) do |a|
  sum = 500 * a
  if sum > X
    next
  elsif sum == X
    count += 1
  else
    B.step(0, -1) do |b|
      sum = 500 * a + 100 * b
      if sum > X
        next
      elsif sum == X
        count += 1
      else
        if ( X - sum ) % 50 == 0
          if ( X - sum ) / 50 <= C
            count += 1
          end
        end
      end
    end
  end
end

puts count

結果

スクリーンショット 2019-11-28 11.10.58.png

1
1
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
1
1