AOJ ITP1 4.Computation
はい。
http://judge.u-aizu.ac.jp/onlinejudge/topic.jsp?cid=ITP1#problems/ITP1_4
A.A / B Problem
2つの整数 a と b を読み込んでa/b(整数)、a%b(整数)、a/b(浮動小数点数)を計算
a,b=map(int,raw_input().split()) print a/b,a%b,'%.8f'%(1.*a/b)
c++11
#include<bits/stdc++.h> #include<vector> #include<algorithm> using namespace std; int main(){ double pai=3.141592653589,da,db; int a,b; scanf("%d %d",&a,&b); da=static_cast<double>(a); db=static_cast<double>(b); printf("%d %d %.8lf\n",a/b,a%b,da/db); return 0; }
別にC++でも1.0を掛ければdoubleに変換されてると思うけど使ったことのない手法でda=static_cast<double>(a);
を使ってキャストして浮動小数点数の計算をした。
B.Circle
半径 r の円の面積と円周の長さを求める
from decimal import Decimal p=3.141592653589 r=float(raw_input()) print Decimal(r*r*p),Decimal(r*2*p)
c++11
#include<bits/stdc++.h> #include<vector> #include<algorithm> using namespace std; int main(){ double pai=3.141592653589; double r; scanf("%lf",&r); printf("%.8lf %.8lf\n",r*r*pai,r*2*pai); return 0; }
入力で与えられのが実数であることと、小数の計算の精度に注意を。
C.Simple Calculator
2つの整数 a, b と1つの演算子 op を読み込んで、a op b を計算するプログラムを作成
while 1: n=raw_input() if '?' in n: break else: print eval(n.replace(' ',''))
C++11
#include<bits/stdc++.h> #include<vector> #include<algorithm> using namespace std; int main(){ double pai=3.141592653589; int a,b; char op; for(;;){ scanf("%d %s %d",&a,&op,&b); if(op=='?'){ break; }else if(op=='+'){ printf("%d\n",a+b); }else if(op=='-'){ printf("%d\n",a-b); }else if(op=='*'){ printf("%d\n",a*b); }else if(op=='/'){ printf("%d\n",a/b); } } return 0; }
びっくりしたのがpythonで http://judge.u-aizu.ac.jp/onlinejudge/review.jsp?rid=1176693#1 ってinput()で計算出来てるらしくて全く知らなかった。。。C++11では特に異常な解法はなかったっぽいです。
D.Min, Max and Sum
n 個の整数 ai(i=1,2,...n)を入力し、それらの最小値、最大値、合計値を求める
n=int(raw_input()) if n: l=[int(x) for x in raw_input().split()] print min(l),max(l),sum(l) else: print "0 0 0"
C++11
#include<bits/stdc++.h> #include<vector> #include<algorithm> using namespace std; int main(){ double pai=3.141592653589; int a; long long s=0; scanf("%d",&a); vector<long>ar(a); for(auto&e:ar){ scanf("%ld",&e); } sort(ar.begin(),ar.end()); for(int i=0;i<a;i++){ s+=ar[i]; } printf("%ld %ld %lld\n",ar[0],ar[a-1],s); return 0; }
vectorではmax,min,sumとかで一発で取り出したりは出来ないらしいので自前で比較したり、ソートで両端を見たりする必要があるっぽい