鱼C论坛

 找回密码
 立即注册
查看: 1120|回复: 4

[已解决]平面几何问题之三角形的整数点

[复制链接]
发表于 2020-11-28 17:02:17 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
本帖最后由 乐乐学编程 于 2020-11-28 21:47 编辑

求在一个已知三个顶点的三角形内有多少个横坐标与纵坐标都是整数的点
最佳答案
2020-11-28 19:32:41
  1. #include <stdio.h>
  2. #include<math.h>

  3. double S_tri(double ax, double ay, double bx, double by, double cx, double cy)
  4. {
  5.         double S;
  6.         S = 0.5 * (ax * (by - cy) + bx * (cy - ay) + cx * (ay - by));
  7.         if (S >= 0)
  8.                 return S;
  9.         else
  10.                 return (-1.0 * S);
  11. }


  12. void main()

  13. {
  14.         int x, y, count = 0;
  15.         double max1, min1, max2, min2;
  16.         double S, S1, S2, S3, ax, ay, bx, by, cx, cy;
  17.         scanf("%lf%lf%lf%lf%lf%lf", &ax, &ay, &bx, &by, &cx, &cy);
  18.         S = S_tri(ax, ay, bx, by, cx, cy);
  19.         max1 = ax;
  20.         min1 = ax;
  21.         if (max1 < bx)
  22.                 max1 = bx;
  23.         if (max1 < cx)
  24.                 max1 = cx;
  25.         if (min1 > bx)
  26.                 min1 = bx;
  27.         if (min1 > cx)
  28.                 min1 = cx;
  29.         max2 = ay;
  30.         min2 = ay;
  31.         if (max2 < by)
  32.                 max2 = by;
  33.         if (max2 < cy)
  34.                 max2 = cy;
  35.         if (min2 > by)
  36.                 min2 = by;
  37.         if (min2 > cy)
  38.                 min2 = cy;

  39.         for (x = min1;x <= max1;x++)
  40.         {
  41.                 for (y = min2;y <= max2;y++)
  42.                 {
  43.                         S1 = S_tri(x, y, bx, by, cx, cy);
  44.                         S2 = S_tri(ax, ay, x, y, cx, cy);
  45.                         S3 = S_tri(ax, ay, bx, by, x, y);
  46.                         if (fabs(S - (S1 + S2 + S3)) < 0.000001)
  47.                                 count++;
  48.                 }
  49.         }
  50.         printf("在给定三点的三角形内有整数点 %d 个\n", count);
  51. }
复制代码

本帖被以下淘专辑推荐:

小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2020-11-28 19:32:41 | 显示全部楼层    本楼为最佳答案   
  1. #include <stdio.h>
  2. #include<math.h>

  3. double S_tri(double ax, double ay, double bx, double by, double cx, double cy)
  4. {
  5.         double S;
  6.         S = 0.5 * (ax * (by - cy) + bx * (cy - ay) + cx * (ay - by));
  7.         if (S >= 0)
  8.                 return S;
  9.         else
  10.                 return (-1.0 * S);
  11. }


  12. void main()

  13. {
  14.         int x, y, count = 0;
  15.         double max1, min1, max2, min2;
  16.         double S, S1, S2, S3, ax, ay, bx, by, cx, cy;
  17.         scanf("%lf%lf%lf%lf%lf%lf", &ax, &ay, &bx, &by, &cx, &cy);
  18.         S = S_tri(ax, ay, bx, by, cx, cy);
  19.         max1 = ax;
  20.         min1 = ax;
  21.         if (max1 < bx)
  22.                 max1 = bx;
  23.         if (max1 < cx)
  24.                 max1 = cx;
  25.         if (min1 > bx)
  26.                 min1 = bx;
  27.         if (min1 > cx)
  28.                 min1 = cx;
  29.         max2 = ay;
  30.         min2 = ay;
  31.         if (max2 < by)
  32.                 max2 = by;
  33.         if (max2 < cy)
  34.                 max2 = cy;
  35.         if (min2 > by)
  36.                 min2 = by;
  37.         if (min2 > cy)
  38.                 min2 = cy;

  39.         for (x = min1;x <= max1;x++)
  40.         {
  41.                 for (y = min2;y <= max2;y++)
  42.                 {
  43.                         S1 = S_tri(x, y, bx, by, cx, cy);
  44.                         S2 = S_tri(ax, ay, x, y, cx, cy);
  45.                         S3 = S_tri(ax, ay, bx, by, x, y);
  46.                         if (fabs(S - (S1 + S2 + S3)) < 0.000001)
  47.                                 count++;
  48.                 }
  49.         }
  50.         printf("在给定三点的三角形内有整数点 %d 个\n", count);
  51. }
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-11-28 19:39:10 | 显示全部楼层
你的问题都具有挑战性,我回复你第一个问题,被审核了,此题目做完后,就没敢再发了
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2020-11-28 21:46:36 | 显示全部楼层
风过无痕1989 发表于 2020-11-28 19:39
你的问题都具有挑战性,我回复你第一个问题,被审核了,此题目做完后,就没敢再发了

多谢了!
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-11-28 22:45:04 | 显示全部楼层
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-5-9 03:49

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表