0
0

More than 3 years have passed since last update.

C - Buy an Integer

Posted at

問題

https://atcoder.jp/contests/abc146/tasks/abc146_c
スクリーンショット 2019-12-04 23.08.14.png

回答

A,B,X = gets.chomp.split.map(&:to_i)
result = 0
max = 1000000000
if A + B > X
elsif X >= A * max + B * ( max.to_s.length )
  result = max
else
  N = X.to_s.length
  N.step( 1, -1 ) do |n|
    p =  A * ( 10 ** ( n - 1 ) ) + B * n
    if p <= X
      result = ( X - B * n ) / A
      if result.to_s.length != n
        result -= 1
      end
      break
    end
  end
end
p result

結果

スクリーンショット 2019-12-04 23.09.39.png

感想

result = ( X - B * ( n + 1 ) ) / Aで割り切れちゃって、桁が1つ多くなってしまった場合がわからなくて一生悩んでしまった・・・

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