AtCoderやれ!!!!!って言われたので
問題はこれ↓
https://atcoder.jp/contests/abs/tasks/abc087_b
問題
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
結果
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
結果
まとめ
処理時間が 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