結果

問題 No.297 カードの数式
コンテスト
ユーザー siman
提出日時 2016-03-26 01:38:20
言語 Ruby
(4.0.2)
コンパイル:
ruby -w -c _filename_
実行:
ruby _filename_
結果
AC  
実行時間 55 ms / 1,000 ms
コード長 817 bytes
記録
記録タグの例:
初AC ショートコード 純ショートコード 純主流ショートコード 最速実行時間
コンパイル時間 281 ms
コンパイル使用メモリ 9,088 KB
実行使用メモリ 14,848 KB
最終ジャッジ日時 2026-05-31 20:26:54
合計ジャッジ時間 2,695 ms
ジャッジサーバーID
(参考情報)
judge3_0 / judge2_0
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 23
権限があれば一括ダウンロードができます
コンパイルメッセージ
Main.rb:3: warning: assigned but unused variable - n
Syntax OK

ソースコード

diff #
raw source code

class Yukicoder
  def initialize
    n = gets.to_i
    c = gets.chomp.split

    nums = c.select{|i| i =~ /\d/}.sort.reverse
    operation = c.select{|i| i =~ /(\+|\-)/}.sort

    divide_num = operation.size + 1

    list = [nums[0..-divide_num].join, nums[-divide_num+1..-1]].flatten

    if operation.include?('-')
      puts [eval(list.zip(operation).join), eval(list.reverse.zip(operation).join)].join(' ')
    else
      max_val = eval(list.zip(operation).join)
      a = Array.new(operation.size+1){""}
      mod = operation.size + 1
      index = 0

      while !nums.empty?
        num = nums.pop
        a[index%mod] = a[index%mod] + num
        index += 1
      end

      a = a.map{|i| i.to_i(10)}

      min_val = eval(a.join('+'))

      puts [max_val, min_val].join(' ')
    end
  end
end

Yukicoder.new
0