毛のはえたようなもの

インターネット的なものをつらつらとかきつらねる。

2007-10-01から1ヶ月間の記事一覧

精度を要する割り算(Rubyの場合)

File.open("../data_b.txt","r"){|f| f.each{|line| item = line.split.collect{|i| i.to_i} if item[0] !=0 && item[1] !=0 printf("%0.10f\n",item[0].to_f/item[1]) end } }

C/C++/Python/Ruby比較2

精度を要する割り算のプログラムの言語比較。 この場合の要求する精度は小数第10位。ただし、割り切れる場合はこの限りでない。 読み込むファイルは以下の形式とする。途中割られる数に0は含まれないとする。 100 25 45 3 2 5 0 0 // 最後の行は「0 0」

標準入力から一行づつ読み込み最大値を表示(Pythonの場合)

#! /user/local/bin/python first=0 for line in open('../data_a.txt','r'): num = int(line) if first !=1 or max < num: max = num first =1 print max

標準入力から一行づつ読み込み最大値を表示(C++の場合)

#include <iostream> using namespace std; int main(int argc,char* agrc[]){ int max, num; bool flag = false; while(cin){ cin >> num; if(flag ==false || max < num){ flag =true; max = num; } } cout << max << endl; return 0; }</iostream>

標準入力から一行づつ読み込み最大値を表示(Cの場合)

#include <stdio.h> #include <string.h> int main(){ int max; char buf[1000]; char* num; int flag = 0; while(fgets(buf, 1000,stdin) != NULL){ num = strtok(buf," \n"); while(num != NULL){ if(flag == 0 || max < atoi(num)){ flag = 1; max = atoi(num); } num =strto</string.h></stdio.h>…

標準入力から一行づつ読み込み最大値を表示(Rubyの場合)

ar = Array.new File.open("../data_a.txt","r"){|f| f.each{|num| ar << num.chomp!.to_i } } p ar.max

C/C++/Python/Ruby比較1

「標準入力から一行づつ読み込み最大値を表示」の言語による比較。読み込むファイルは改行区切りの数が保存されているものとする。