毛のはえたようなもの

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

2007-01-01から1年間の記事一覧

右側折り返し(Pythonの場合)

#! /user/local/bin/python maxlength = 72 text="" def insert_sp(i,text,line): if i!=0 and (i)%maxlength ==0: text = text + "\n" if line[i] == "\n": print text else: text = text + line[i] insert_sp(i+1,text,line) for line in open('../data_c.…

右側折り返し(c++の場合)

#include <iostream> using namespace std; int main(void){ int num=0; char word; int maxlength=71; while(cin.get(word)){ if(word == '\n'){ num=0; }else if(num > maxlength){ cout << endl; num=0; }else{ num++; } cout << word; } return 0; }</iostream>

右側折り返し(cの場合)

#include <stdio.h> int main(void){ int num=0; char word; FILE *stream; int maxlength=71; stream = fopen("../data_c.txt","r"); while((word = getc(stream)) != EOF){ if(word == '\n'){ num=0; }else if(num > maxlength){ printf("\n"); num=0; }else{ num++</stdio.h>…

C/C++/Python/Ruby比較3

読み込んだファイルの内容を表示する。ただし、指定した文字数で改行すること。(以下は一行72文字の場合)

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

#!/usr/bin/env python import sys for line in open ('../data_b.txt','r'): item = line.split(' ') if float(item[0])!=0 and float(item[1])!=0: print float(item[0])/float(item[1])

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

#include <iostream> #include <iomanip> using namespace std; int main(int argc,char* agrc[]){ double a; double b; while(true){ cin >> a; cin >> b; if(a==0 && b==0){ break; }else{ cout << setprecision(10) << a/b << endl; } } return 0; }</iomanip></iostream>

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

#include <stdio.h> int main(){ char string[10000]; double a, b; while(fgets(string, 10000,stdin) != NULL){ sscanf(string,"%d %d",&a,&b); if(a==0 && b==0){ break; }else{ printf("%0.10f\n",a/b); } } return 0; }</stdio.h>

精度を要する割り算(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

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