|
|
back to boardDiscussion of Problem 1020. RopeWrong answer why Posted by zarrafa 10 Oct 2009 22:22 #include <stdio.h> #include <math.h> int main(){ freopen("b.in","r",stdin); freopen("b.out","w",stdout); double x[102],y[102]; double d1,d2; int i,star=0,k,m,n,j; double s,r,pi=2*acos(0); scanf("%d",&k); for(m=0;m<k;m++){ s=0; scanf("%d %Lf",&n,&r); for(i=0;i<n;i++) scanf("%Lf %Lf \n",&x[i],&y[i]); for(j=0;j<n-1;j++){ d1=abs(x[j]-x[j+1]); d2=abs(y[j]-y[j+1]); s=s+sqrt(d1*d1 + d2*d2); } d1=abs(x[0]-x[n-1]); d2=abs(y[0]-y[n-1]); s=s+sqrt(d1*d1 + d2*d2); s=s+(n*pi*r)/2; if(star==0) star=1; else printf("\n"); printf("%.2Lf",s); } return 0; } Re: Wrong answer why #include <iostream> #include <cmath> using namespace std; int main(){ double PI=acos(-1.0); int N; double r(0), perimeter(0); cin>>N>>r; double *pointx, *pointy; pointx = new double[N+1]; pointy = new double[N+1]; for(int a=0; a<N; a++){ cin>>pointx[a]>>pointy[a]; } pointy[N]= pointx[0]; pointx[N] = pointy[0]; cout.setf(ios::fixed); cout.setf(ios::showpoint); cout.precision(2); if(N==1){ perimeter=2*PI*r; cout<<perimeter;
} else{ if(N==2){ perimeter=2*(pow((pow((pointx[0]-pointx[1]),2)+pow((pointy[0]-pointy[1]),2)),0.5)) + 2*PI*r; cout<<perimeter;
} else{ double angle(0); for(int a=0; a<N; a++){ perimeter += pow((pow((pointx[a]-pointx[a+1]),2)+pow((pointy[a]-pointy[a+1]),2)),0.5); }
double p1x, p2x, p3x, p1y, p2y, p3y, m1, m2;
p1x=pointx[N-1]; p2x=pointx[0]; p3x=pointx[1]; p1y=pointy[N-1]; p2y=pointy[0]; p3y=pointy[1]; if(p3x==p2x) m1=tan(PI/2.0); if(p2x==p1x) m2=tan(PI/2.0); if(p2x!=p3x) m1=(p3y-p2y)/(p3x-p2x); if(p2x!=p1x) m2=(p1y-p2y)/(p1x-p2x); angle = 2*PI - (PI + fabs(atan(m1)-atan(m2))); for(int a=1; a<N; a++){ p1x=pointx[a-1]; p2x=pointx[a]; p3x=pointx[a+1]; p1y=pointy[a-1]; p2y=pointy[a]; p3y=pointy[a+1]; if(p3x==p2x) m1=tan(PI/2.0); if(p2x==p1x) m2=tan(PI/2.0); if(p2x!=p3x) m1=(p3y-p2y)/(p3x-p2x); if(p2x!=p1x) m2=(p1y-p2y)/(p1x-p2x);
angle += ((2*PI) - (PI + fabs(atan(m1)-atan(m2)))); }
perimeter += r*angle; cout<<perimeter; }} delete pointx; delete pointy; return 0;}
// even me wrong answer Edited by author 13.01.2010 20:27 Edited by author 13.01.2010 20:27 |
|
|