結果

問題 No.297 カードの数式
ユーザー Naoki_M_Naoki_M_
提出日時 2015-11-06 23:29:06
言語 Ruby
(3.2.2)
結果
AC  
実行時間 82 ms / 1,000 ms
コード長 722 bytes
コンパイル時間 476 ms
コンパイル使用メモリ 11,224 KB
実行使用メモリ 15,280 KB
最終ジャッジ日時 2023-08-26 22:43:32
合計ジャッジ時間 3,094 ms
ジャッジサーバーID
(参考情報)
judge13 / judge15
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 75 ms
15,244 KB
testcase_01 AC 82 ms
15,276 KB
testcase_02 AC 82 ms
15,048 KB
testcase_03 AC 76 ms
15,156 KB
testcase_04 AC 75 ms
15,104 KB
testcase_05 AC 77 ms
15,160 KB
testcase_06 AC 75 ms
15,096 KB
testcase_07 AC 76 ms
15,276 KB
testcase_08 AC 76 ms
14,992 KB
testcase_09 AC 77 ms
15,192 KB
testcase_10 AC 75 ms
15,096 KB
testcase_11 AC 75 ms
14,992 KB
testcase_12 AC 75 ms
15,160 KB
testcase_13 AC 76 ms
15,160 KB
testcase_14 AC 75 ms
15,280 KB
testcase_15 AC 77 ms
15,248 KB
testcase_16 AC 76 ms
15,052 KB
testcase_17 AC 75 ms
15,188 KB
testcase_18 AC 74 ms
15,248 KB
testcase_19 AC 76 ms
15,164 KB
testcase_20 AC 76 ms
15,160 KB
testcase_21 AC 74 ms
15,156 KB
testcase_22 AC 75 ms
15,176 KB
testcase_23 AC 75 ms
15,088 KB
testcase_24 AC 75 ms
15,136 KB
testcase_25 AC 76 ms
14,984 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
Main.rb:1: warning: assigned but unused variable - n
Syntax OK

ソースコード

diff #

n = gets.chomp.to_i
cs = gets.chomp.split

hs = Hash.new(0)
cs.map { |c| hs[c] += 1 if c.to_i.to_s != c }
cs = cs.select! { |c| c.to_i.to_s == c }.sort!
ds = cs

max = 0
hs['-'].times do
  max -= cs[0].to_i
  cs = cs.drop(1)
end
hs['+'].times do
  max += cs[0].to_i
  cs = cs.drop(1)
end
max += cs.reverse.inject (0) { |m, x| m * 10 + x.to_i}

min = 0
if 0 < hs['-']
  min += ds[0].to_i
  ds = ds.drop(1)
  hs['+'].times do
    min += ds[0].to_i
    ds = ds.drop(1)
  end
  (hs['-'] - 1).times do
    min -= ds[0].to_i
    ds = ds.drop(1)
  end
  min -= ds.reverse.inject (0) { |m, x| m * 10 + x.to_i}
else
  ds.reverse.each_with_index do |d, i|
    min += d.to_i * 10** (i / (hs['+'] + 1))
  end
end

puts "#{max} #{min}"
0