あんまり書いてないな
最近仕事ではPHPばかりなのでRubyを使っていないのでACM/ICPC国内予選突破の手引きの問題をRubyでといてみる。
Hanafuda Shuffle
Array#[start, length]で一発で解ける件について
# 1978 # hanafuda shuffle def shuffle(n,r) deck = (1..n).to_a.reverse r.times do p, c = STDIN.readline.split(' ').map {|n| n.to_i } pth = deck[0, p - 1] cth = deck[p-1,c] deck[0,c] = cth deck[c,p-1] = pth end deck[0] end def hanafuda_shuffle results = [] loop do n, r = STDIN.readline.split(' ').map {|n| n.to_i } return results if n == 0 and r == 0 results << shuffle(n, r) end end puts hanafuda_shuffle.join("\n")
Ohgas' Fortune
総当りで最高のものを抽出。
ぜんぜんオブジェクト指向のプログラムじゃない。。。プログラマに向いていないんじゃないかと思える。。
# 2683 # ohgas' fortune require 'pp' class Operation def initialize(interest, rate, charge) @interest = interest @rate = rate @charge = charge end attr_reader :rate attr_reader :charge def simple? @interest == 0 end def compound? @interest == 1 end end def search_best_operation fund = STDIN.readline.to_i years = STDIN.readline.to_i operations = [] n = STDIN.readline.to_i n.times do i, r, c = STDIN.readline.split(' ') operations << Operation.new(i.to_i, r.to_f, c.to_i) end search(fund, years, operations) end def search(fund, years, operations) operations.map do |operation| calc(fund, years - 1, operations, operation, 0) end.max end def calc(fund, years, operations, operation, acc_interest) interest = (fund * operation.rate).to_i acc_interest += interest if operation.simple? fund += interest if operation.compound? fund -= operation.charge return fund + acc_interest if years == 0 operations.map do |operation| calc(fund, years - 1, operations, operation, acc_interest) end.max end def main results = [] STDIN.readline.to_i.times do results << search_best_operation end puts results.join("\n") end main
keitai message
# # keitai message class Button def initialize(table) @table = table reset end def reset @index = -1 end def push @index += 1 end def peek @table[@index % @table.size] end def pop p = peek reset p end end class Keitai def initialize @buttons = [ nil, Button.new(['.', ',', '!', '?', ' ']), Button.new(['a', 'b', 'c']), Button.new(['d', 'e', 'f']), Button.new(['g', 'h', 'i']), Button.new(['j', 'k', 'l']), Button.new(['m', 'n', 'o']), Button.new(['p', 'q', 'r', 's']), Button.new(['t', 'u', 'v']), Button.new(['w', 'x', 'y', 'z']), ] @buffer = '' @target = nil end def push(n) if n == 0 return if @target == nil @buffer << @target.pop @target = nil return end if @target == nil @target = @buttons[n] end @target.push end def to_s @buffer end end def main results = [] STDIN.readline.to_i.times do keitai = Keitai.new STDIN.readline.split(//).map {|n| n.to_i}.each {|n| keitai.push n } results << keitai.to_s end puts results.join("\n") end main