LoginSignup
0
0

More than 5 years have passed since last update.

第19回オフラインリアルタイムどう書くの参考問題をRubyで解く

Posted at

問題は
http://nabetani.sakura.ne.jp/hena/ord19sanwa/

def solve(q)
  nums = q.split(',').map(&:to_i)
  max_num = nums.max
  sums = []
  (1..max_num).each {|x|
    (x..max_num).each {|y|
      (y..max_num).each {|z|
        sum = [x,y,z,x*2,y*2,z*2,x*3,y*3,z*3]
        [x,y,z].combination(2){|a,b|
          sum << a + b
        }
        [x,x,y,y,z,z].combination(3){|a,b,c|
          sum << a + b + c
        }
        if ((nums & sum) == nums)
            sums << [x,y,z]
        end
      }
    }
  }
  case sums.size
  when 0
    'none'
  when 1
    sums[0].join(',')
  else
    'many'
  end
end

puts Time.now
DATA.readlines.each do |line|
  no,q,a = line.chop.split(/\s+/)
  ans = solve(q)
  print no + "\t" + ans
  puts ans == a ? ' o' : ' x'
end
puts Time.now

__END__
0   3,11,12,102,111,120 1,10,100
1   10,20,30,35,70  many
2   1,5,20,80   none
3   1,2,3,4,5,6,7,8,9,10,11,12,13,14    many
4   1,2,3,4,5,6,7,8,9,10,11,12,13,14,15 1,4,5
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