AOJ ITP1 10.Math Functions
はい。
http://judge.u-aizu.ac.jp/onlinejudge/topic.jsp?cid=ITP1#problems/ITP1_10
A.Distance
(n,m,o,p) = map(float, raw_input().split()) print ((n-o)**2 + (m-p)**2)**0.5
C++11
#include<bits/stdc++.h> #include<vector> #include<algorithm> using namespace std; double di(double a,double b,double c,double d){return sqrt((c-a)*(c-a)+(d-b)*(d-b));} int main(){ double pai=3.141592653589; double x1,y1,x2,y2,ans; scanf("%lf %lf %lf %lf",&x1,&y1,&x2,&y2); printf("%lf\n",di(x1,y1,x2,y2)); return 0; }
入力が実数で与えられることが注意、平方根の計算は特に工夫しなくてもWAになるほどの誤差は出ないと思う。
B.Triangle
import math a,b,C=map(float,raw_input().split()) rad=math.radians(C) c=(a**2+b**2-2*a*b*math.cos(rad))**0.5 h=b*math.sin(rad) print h*a/2 print a+b+c print h
C++11
#include<bits/stdc++.h> #include<vector> #include<algorithm> using namespace std; int main(){ double pai=3.141592653589; int a,b,C; double S,L,h,rad,cs; scanf("%d %d %d",&a,&b,&C); rad=C*pai/180; cs=sqrt(a*a+b*b-2*a*b*cos(rad)); h=b*sin(rad); printf("%.8lf\n%.8lf\n%.8lf\n",h*a/2,a+b+cs,h); return 0; }
すっかり忘れてるので後でもう一度掘り下げる。これは三角関数とか計算知ってないと無理だと思う。
C.Standard Deviation
while True: n = int(raw_input()) if n==0: break l = map(float, raw_input().split()) avg=sum(l)/len(l) naka=0 for i in l: naka+=(i-avg)**2 hyo2 = naka/n print (hyo2)**0.5
C++11
#include<bits/stdc++.h> #include<vector> #include<algorithm> using namespace std; int main(){ double pai=3.141592653589; int n,tmp; int s[1000]; double avg,total,ans; for(;;){ scanf("%d",&n); if(n==0) break; total=0; ans=0; for(int j=0;j<n;j++){ scanf("%d",&s[j]); total+=s[j]; } avg=total/n; for(int k=0;k<n;k++){ ans+=(s[k]-avg)*(s[k]-avg); } ans=sqrt(ans/n); printf("%.12lf\n",ans); } return 0; }
問題文に示されている式の通りに\(s_i\)と平均の差の2乗の総和をnで割ったものの平方根が解になるはず。
D.Distance II
#後で
C++11
#include<bits/stdc++.h> #include<vector> #include<algorithm> using namespace std; int main(){ double pai=3.141592653589; int n; int x[1000],y[1000]; double ans1=0,ans2=0,ans3=0,ans4=0,anstmp=0,tmp; scanf("%d",&n); for(int i=0;i<2;i++){ for(int j=0;j<n;j++){ if(i==0) scanf("%d",&x[j]); else scanf("%d",&y[j]); } } for(int i=0;i<n;i++){ ans1+=abs(x[i]-y[i]); } printf("%.8lf\n",ans1); for(int i=0;i<n;i++){ ans2+=(x[i]-y[i])*(x[i]-y[i]); } printf("%.8lf\n",pow(ans2,1.0/2.0)); for(int i=0;i<n;i++){ tmp=abs(x[i]-y[i]); ans3+=tmp*tmp*tmp; } printf("%.8lf\n",pow(ans3,1.0/3.0)); for(int i=0;i<n;i++){ anstmp=abs(x[i]-y[i]); ans4=max(ans4,anstmp); } printf("%.8lf\n",ans4); return 0; }
p=3の時は問題文の式の2乗の部分が3乗になり、解がその立方根になる。C++ではpowを使って求めました。