zju 1439. Wrong Answer, Why? 请LeeMaRS等作过的过来看一下。

孩皮妞野 2003-02-19 01:29:14
上面写exact to three digits to the right of the decimal point精确到小数点后三位。实际上是4舍5入到小数点后3位。

样题我算到0.171, 还以为是计算过程中舍入误差太大,推导出r^2/R^2的计算公式。

s/S = r^2/R^2
1
= ---------------------------------------------------------
/ 1 1 \^2
b^2*c^2*| ---------------- + ------------------ |
\ 2ac-a^2-c^2+b^2 2ab-a^2-b^2+c^2 /
其中s、r是小园面积和半径;S、R是大园面积和半径。a,b,c是三角形3条边的长度。

使用doulbe,这个算式的精度应该足够了。可是还是Wrong Answer.

请问你们是怎样解的?

...全文
286 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
laomai 2003-02-21
  • 打赏
  • 举报
回复
LeeMaRS(想做DP杀手的小菜虎),多谢!
LeeMaRS 2003-02-20
  • 打赏
  • 举报
回复
acm.zju.edu.cn
laomai 2003-02-20
  • 打赏
  • 举报
回复
请教一下,zju到底是什么网址?
孩皮妞野 2003-02-20
  • 打赏
  • 举报
回复
我上面的公式也是对的。而且可能误差更小。我不明白的是为什么Wrong Answer. 准备用 BambooTang(竹子)兄的C++程序用样本数据测试一下。

谢谢两位。
LeeMaRS 2003-02-19
  • 打赏
  • 举报
回复
ZJU 1439 - Area Ratio - 00:00.07 676K
http://acm.zju.edu.cn/show_problem.php?pid=1439
AC 1次
这道题不难,只是看上去有点吓人。题目给的是三维空间中的一个三角形三个顶点的坐标,由于三点确定一个平面,最后肯定还是转化到平面上的问题上来。剩下的事情就是套套公式而已了。这些公式还是很常用的,我收集在另一个文档中了。
----------------------------------
设任意一个三角形的内接圆圆心为O, 三边分别为a, b, c, 内接圆半径为r. 根据面积关系, 有:

S(ABC) = S(OAB) + S(OBC) + S(OAC)
= r(a+b+c)/2;

所以就有 r = (2S)/(a+b+c);
--------------------------------------
设任意一个三角形的三边为a, b, c, 边a所对的角为A. 所求外接圆半径为R

根据任意三角形面积公式, 得到 bcsinA/2 = S

再根据正弦定理, 得到 2RsinA = a

两式一除, 得到 (bc/2) / (2R) = S/a

整理, 就得到 R = abc/(4S)
----------------------------------
求三角形的面积, 用海伦公式就可以了.

Program T_1439;
Var
x1,y1,z1,x2,y2,z2,x3,y3,z3 : Double;
a, b, c, R1, R2, S, P : Double;

Function Dist(x1, y1, z1, x2, y2, z2 : Double) : Double;
Begin
Dist:=Sqrt(Sqr(x1-x2)+Sqr(y1-y2)+Sqr(z1-z2));
End;

Begin
While NOT EOF Do
Begin
Readln(x1,y1,z1,x2,y2,z2,x3,y3,z3);
a := Dist(x1,y1,z1,x2,y2,z2);
b := Dist(x1,y1,z1,x3,y3,z3);
c := Dist(x2,y2,z2,x3,y3,z3);
P := (a+b+c)/2;
S := Sqrt(P*(P-a)*(P-b)*(P-c));
R1 := a*b*c/(4*S);
R2 := S/P;
Writeln(Sqr(R2)/Sqr(R1):0:3);
End;
End.
BambooTang 2003-02-19
  • 打赏
  • 举报
回复
海伦公式
BambooTang 2003-02-19
  • 打赏
  • 举报
回复
好像我的程序比较那个 嗷嗷~~

//#define DEBUG
#include <iostream>
#include <iomanip>
#include <math.h>

#ifdef DEBUG
#include <fstream>
#endif

using namespace std;

#ifdef DEBUG

ifstream fin("in.txt");
ofstream fdebug("debug.txt");
istream& in = fin;
ostream& out = cout;
#else
istream& in = cin;
ostream& out = cout;
#endif

int ax,ay,az;
int bx,by,bz;
int cx,cy,cz;

inline double dist(double x1,double y1,double z1,double x2,double y2,double z2){
return sqrt((x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2) + (z1 - z2) * (z1 - z2));
}
int main()
{
double res_area;
double r;
double R;
double p;
double len1,len2,len3;

out.setf(ios::fixed);
out.precision(3);
while(in >> ax >> ay >> az >> bx >> by >> bz >> cx >> cy >> cz){
len1 = dist(ax,ay,az,bx,by,bz);
len2 = dist(bx,by,bz,cx,cy,cz);
len3 = dist(cx,cy,cz,ax,ay,az);
p = (len1 + len2 + len3) / 2.0;
res_area = sqrt(p*(p-len1) * (p-len2) * (p-len3));
r = 2 * res_area / ( len1 + len2 + len3);
R = len1 * len2 * len3 / 4.0 / res_area;
out << (r * r / R / R ) << endl;
}

return 0;
}
孩皮妞野 2003-02-19
  • 打赏
  • 举报
回复
我的程序:

//---------------------------------------------------------------------------
#include <iostream>
#include <cmath>

typedef double float_t;

struct point3d{
int x,y,z;
point3d(){};
point3d(int x,int y,int z):x(x),y(y),z(z){}

unsigned sq_distance(const point3d& rhs)const{
int dx,dy,dz;
dx = x-rhs.x;
dy = y-rhs.y;
dz = z-rhs.z;

return dx*dx+dy*dy+dz*dz;
}
friend std::istream& operator>>(std::istream& os, point3d& p);
};

std::istream& operator >>(std::istream& is, point3d& p)
{
is >>p.x>>p.y>>p.z;
return is;
}

struct triangle{
point3d A,B,C;

triangle(){}
//triangle(const point3d& A

float_t area_ratio()const{
float_t a2,b2,c2,a,b,c;
a2 = B.sq_distance(C);
b2 = A.sq_distance(C);
c2 = A.sq_distance(B);
a = std::sqrt(a2);
b = std::sqrt(b2);
c = std::sqrt(c2);
float_t tmp = 1/(2*a*c-a2-c2+b2)+1/(2*a*b-a2-b2+c2);
tmp = (1/(b2*c2*tmp*tmp)+.0005;

return int(tmp*1000)/1000.0;
}
};
//---------------------------------------------------------------------------
int main()
{
triangle t;
while(std::cin>>std::ws,!std::cin.eof()){
std::cin>>t.A>>t.B>>t.C;
std::cout<<t.area_ratio()<<std::endl;
}
return 0;
}
//---------------------------------------------------------------------------




33,010

社区成员

发帖
与我相关
我的任务
社区描述
数据结构与算法相关内容讨论专区
社区管理员
  • 数据结构与算法社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧