#!/usr/koeki/bin/ruby # coding: utf-8 require "./kprintf.rb" point = Hash.new while yline = gets if /(\S+)\s+(\d+)\s+(\d+)/ =~ yline # 1個目の() (\S+)→氏名が入る # 2個目の() (\d+)→国語の得点が入る # 3個目の() (\d+)→数学の得点が入る j = $2.to_i # 国語の得点 m = $3.to_i # 数学の得点 point[$1] = [j, m, j+m] # [国語得点, 数学得点, 合計] の配列を代入 end end print "--氏名--------------+-国語-+-数学---\n" for student in point.keys.sort {|x, y| point[x][0] <=> point[y][0] }.reverse # student には、学生の氏名が入っている kokugo = point[student][0] math = point[student][1] printf("%-20s %5d %5d\n", student, kokugo, math) end puts "-"*36