http://acm.whu.edu.cn/noah/problem/problem.jsp?problem_id=1140
武汉大学竞赛题
复数的常数项无穷级数
类似于几何级数 ∑a(n) 其中a(i)=a(1)*q^(i-1) 在|q|<1时收敛于 a(1)/(1-q)
设a,b是复数,∑Z(n) 其中Z(i)=a*b^(i-1) 在|b|<1时收敛于 a/(1-b)
比赛时的超时代码
//lecoo 1140 Time Limit Exceed 1216 1314 G++ 806
#include <iostream>
#include <cmath>
#define PI 3.14159265358979323846
using namespace std;
class problem{
double X;
double Y;
double A;
double k;
double x0;
double y0;
double d;
double alpha0;
public:
problem(double x,double y,double a,double kk){
X=x;Y=y;A=a/180.0*PI;k=kk;
d=sqrt(x*x+y*y);
alpha0=atan2(y,x);
}
void workout()
{
x0=X;
y0=Y;
double dx,dy;
double K=1;
double alpha=alpha0;
for(int i=1;K*d>1e-3;i++){
K*=k;
alpha=fmod(alpha+A,2*PI);
dx=K*d*cos(alpha);
dy=K*d*sin(alpha);
x0+=dx;
y0+=dy;
}
}
void solve(){
workout();
printf("(%.3lf,%.3lf)\n",x0,y0);
}
};
int main()
{
double X,Y,A,k;
while(scanf("%lf%lf%lf%lf",&X,&Y,&A,&k)!=EOF){
problem p(X,Y,A,k);
p.solve();
}
}
赛后搞懂后写的AC的代码
#include <iostream>
#include <cmath>
using namespace std;
const double PI=acos(-1.0);
//求2阶行列式(determinant)的值
inline double dtm(double a11,double a12,double a21,double a22)
{
return a11*a22-a21*a12;
}
int main()
{
double X,Y,A,k;
while(scanf("%lf%lf%lf%lf",&X,&Y,&A,&k)!=EOF){
A=A/180*PI;
double d= dtm(1-k*cos(A), k*sin(A), -k*sin(A), 1-k*cos(A));
double m1=dtm( X, k*sin(A), Y, 1-k*cos(A));
double m2=dtm(1-k*cos(A), X, -k*sin(A), Y);
printf("(%.3lf,%.3lf)\n", m1/d, m2/d);
}
}
使用complex类的代码,在别人的CODE中看到的,觉得自己还有许多要学啊
#include <cstdio>
#include <cmath>
#include <complex>
using namespace std;
typedef complex<double> Comp;
const double PI=acos(-1.0);
int main()
{
double X,Y,A,k;
while(scanf("%lf%lf%lf%lf",&X,&Y,&A,&k)!=EOF){
A=A/180*PI;
Comp a(X,Y);
Comp b(1-k*cos(A),-k*sin(A));
Comp c=a/b;
printf("(%.3lf,%.3lf)\n", c.real(),c.imag() );
}
}