果园里的树
2016-04-16 14:02
344 查看
该题主要用到了三角形的有向面积的知识,还有行列式的一点知识;
需要注意的是判断两浮点数是否相等时,由于浮点运算的误差,只需判断fabs(a-b)的值小于一个极小值即可,如1e-9.应当尽量避免浮点运算,在规定只保留n位小数的情况下,可以将所有坐标乘以10^n将其转换为整数间的运算。。。(摘自书上= =)
需要注意的是判断两浮点数是否相等时,由于浮点运算的误差,只需判断fabs(a-b)的值小于一个极小值即可,如1e-9.应当尽量避免浮点运算,在规定只保留n位小数的情况下,可以将所有坐标乘以10^n将其转换为整数间的运算。。。(摘自书上= =)
//5.4.3 P84 #include <iostream> #include <fstream> #include <cmath> //#define LOCAL using namespace std; struct Point { double x; double y; }; double areaTriangle2(const Point &a, const Point &b, const Point &c) { double res = a.x * b.y + b.x * c.y + a.y * c.x - c.x * b.y - a.y * b.x - a.x * c.y; return res; } int main() { #ifdef LOCAL ifstream fin(""); ofstream fout(""); #define cin fin #define cout fout #endif // LOCAL Point a, b, c; while (cin >> a.x >> a.y >> b.x >> b.y >> c.x >> c.y) { int cnt = 0; for (int i = 1; i <= 99; ++i) { for (int j = 1; j <= 99; ++j) { Point o = {i, j}; double s = fabs(areaTriangle2(a, b, c)); double s1 = fabs(areaTriangle2(a, b, o)); double s2 = fabs(areaTriangle2(a, c, o)); double s3 = fabs(areaTriangle2(b, c, o)); if (fabs(s1 + s2 + s3 - s) < 1e-9) { ++cnt; } } } cout << cnt << endl; } return 0; }
相关文章推荐
- 构建之法阅读笔记04
- Spring注解式与配置文件式
- UnicodeEncodeError: ‘ascii’ codec can’t encode characters in position xxx ordinal not in range(***)
- 正则表达式匹配
- 从斜边之长为L的一切直角三角形中,求有最大周长的直角三角形.(多元函数的极值及其求法)
- 邮件复选框
- 3. Longest Substring Without Repeating Characters
- Struts2之ModelDriven的使用
- iOS 动画总结----UIView动画
- linux下利用线程池构建高并发server
- 在category和protocol中如何实现@property
- vsftpd.conf 详解
- ti processor sdk linux am335x evm /bin/create-sdcard.sh hacking
- CSS完美兼容IE6/IE7/IE8/IE9/IE10的通用方法
- 使用AdBlockPlus在App中过滤网页广告
- PROCESS_YIELD()宏和C语言的switch语句
- listView的属性 Android:cacheColorHint="#00000000"
- HDFS写文件过程分析
- struts2中的ModelDriven使用
- Struts2中的ModelDriven机制及其运用