時間限制:4秒
題目大意:
給定一函數p(x)、區間[x1,x2],求出以"中位數"為基、每0.01單位分割的面積積分。
解法:
利用 Ruby eval函數的特性,做好字串處理後直接交給它處理。
要注意的是必須加入.to_f讓 Ruby 知道是用float進行運算。
以下程式以 Ruby 執行:
# Author : mashimaro
# Last Modify : 2008/01/02
require 'digest/md5'
require 'net/http'
require 'net/https'
host = 'www.bright-shadows.net'
path = '/challenges/programming/peanojordan/tryout.php'
cookie = 'PHPSESSID=**********************************'
http = Net::HTTP.new(host, 80)
#http.use_ssl = true
headers = {
'Cookie' => cookie,
# 'Referer' => 'http://profil.wp.pl/login.html',
# 'Content-Type' => 'application/x-www-form-urlencoded'
}
while(true)
resp, data = http.get(path, headers);
data.sub!('<pre>', '');
data.sub!('</pre>', '');
lines = data.split('<br>');
exp = lines[0].sub('p(x) = ', '').gsub('/','.to_f/').gsub('^','**');
a, b = lines[2].scan(/[\-0-9]+/);
int_1 = a.to_i;
int_2 = b.to_i;
puts '';
puts lines[0];
puts '';
puts lines[2];
puts '';
puts exp;
#puts int_1, int_2;
puts '';
break if(exp[-1..-1].to_i < 6);
end
sum = 0;
int_1 = int_1 + 0.005;
while(int_1 < int_2)
sum = sum + eval(exp.gsub('x','('+int_1.to_s+')'));
puts sum;
int_1 = int_1 + 0.01;
end
puts (sum * 0.01).round;
digest = Digest::MD5.hexdigest((sum * 0.01).round.to_s);
ret_path = '/challenges/programming/peanojordan/solution.php?solution=' + digest;
resp, data = http.get(ret_path, headers);
puts data;
消息來源
0 意見:
張貼留言