問10 Circumscribed Circle of a Triangle
解法
外心のベクトル表示そのまま実装で解きます。
#include<stdio.h>
#include<math.h>
//外心のベクトル表示から座標を求める
double calc_sin2(double x1,double y1,
double x2,double y2,
double x3,double y3){
//sin2Aを求める関数
double dx1,dy1,dx2,dy2,cosA,sinA,len1,len2;
dx1=x2-x1;
dy1=y2-y1;
dx2=x3-x1;
dy2=y3-y1;
len1=sqrt(dx1*dx1+dy1*dy1);
len2=sqrt(dx2*dx2+dy2*dy2);
cosA=(dx1*dx2+dy1*dy2)/(len1*len2);
sinA=(dx1*dy2-dy1*dx2)/(len1*len2);
return 2*cosA*sinA;
}
void calc_point(double x1,double y1,
double x2,double y2,
double x3,double y3){
//外心を求める関数
//外心のベクトル表示をそのまま実装
double sin2A,sin2B,sin2C,sumSin,r;
sin2A=calc_sin2(x1,y1,x2,y2,x3,y3);
sin2B=calc_sin2(x2,y2,x3,y3,x1,y1);
sin2C=calc_sin2(x3,y3,x1,y1,x2,y2);
sumSin=sin2A+sin2B+sin2C;
double ansX,ansY;
ansX=(x1*sin2A+x2*sin2B+x3*sin2C)/sumSin;
ansY=(y1*sin2A+y2*sin2B+y3*sin2C)/sumSin;
r=sqrt((x1-ansX)*(x1-ansX)+(y1-ansY)*(y1-ansY));
printf("%.3lf %.3lf %.3lf\n",ansX,ansY,r);
}
int main(){
int n;
double x1,y1,x2,y2,x3,y3;
scanf("%d",&n);
while(n--){
scanf("%lf %lf %lf %lf %lf %lf",
&x1,&y1,&x2,&y2,&x3,&y3);
calc_point(x1,y1,x2,y2,x3,y3);
}
}
問11 Drawing Lots
解法
横棒の仕方に従って中身を入れ替えるだけで解けます。
#include<stdio.h>
int main(){
int w,n,a,b,Nos[31];
scanf("%d %d",&w,&n);
for(int i=1;i<=w;i++)Nos[i]=i;
while(n--){
scanf("%d,%d",&a,&b);
int t=Nos[a];
Nos[a]=Nos[b];
Nos[b]=t;
}
for(int i=1;i<=w;i++)printf("%d\n",Nos[i]);
}
問12 A Point in a Triangle
解法
三角形ABCの中に点Pがあれば角APB、BPC、CPAは同じ向きに回転角のはずです。
これは三角形APB、BPC、CPAの外積を求めれば正負で判断できます。
#include<stdio.h>
int main(){
double x1,y1,x2,y2,x3,y3,xp,yp;
double s1,s2,s3;
while(scanf("%lf %lf %lf %lf %lf %lf %lf %lf",
&x1,&y1,&x2,&y2,&x3,&y3,
&xp,&yp)!=EOF){
s1=(x1-xp)*(y2-yp)-(x2-xp)*(y1-yp);
s2=(x2-xp)*(y3-yp)-(x3-xp)*(y2-yp);
s3=(x3-xp)*(y1-yp)-(x1-xp)*(y3-yp);
if((s1<0&&s2<0&&s3<0)||(s1>0&&s2>0&&s3>0)){
printf("YES\n");
}else{
printf("NO\n");
}
}
}
問13 Switching Railroad Cars
解法
スタックをそのまま使えば解けます。
入ってきたらスタックに積み、でるならスタックから一つ取り出すだけです。
#include<stdio.h>
#include<stack>
int main(){
std::stack<int> S;
int no;
while(scanf("%d",&no)!=EOF){
if(no==0){
no=S.top();
S.pop();
printf("%d\n",no);
}else{
S.push(no);
}
}
}
問14 Integral
解法
指定されたとおりに計算するだけです。
#include<stdio.h>
int main(){
int dx;
while(scanf("%d",&dx)!=EOF){
int ans=0;
for(int xp=0;xp<600;xp+=dx){
ans+=xp*xp*dx;
}
printf("%d\n",ans);
}
}
最終更新:2014年01月11日 05:36