LoginSignup
5
1

More than 3 years have passed since last update.

roman numerals

Last updated at Posted at 2020-11-25

code

def check_column(i)
  case i
  when 1000
    r = 'M'
    s = ''
  when 100
    r = 'C'
    s = 'D'
  when 10    
    r = 'X'
    s = 'L'
  else
    r = 'I'
    s = 'V'
  end
  return r,s
end

def check_roman(i,q)  
  check = q.to_f / 4
  if check < 1
    r,s = check_column(i)
    r_num = r * q
  elsif check == 1
    r,s = check_column(i)
    r_num = r + s
  elsif check <= 2
    r,s = check_column(i)
    r_num = s + r * (q - 5)
  elsif check > 2
    r,s = check_column(i)
    r_n,s_n = check_column(i * 10)
    r_num = r + r_n
  end
  return r_num
end 

num = ARGV[0].to_i
r_num = ''
if num / 3000 > 1
  p " there aree not roman numerals over 3000"
else
  [1000,100,10,1].each do |i|
    q = num / i
    if q != 0
      r_num += check_roman(i,q)    
      p r_num

    end
    num = num - q * i
  end
end


  • source ~/classes/muli_scale/grad_members_20f/members/keita_k7/memo/l1.org
5
1
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
5
1