鱼C论坛

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

当横纵坐标都位于0<= a <= 2内时,一共有多少个直角三角形?

[复制链接]
发表于 2020-2-26 16:34:30 | 显示全部楼层 |阅读模式

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

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

x
本帖最后由 guoquanli 于 2020-2-26 17:28 编辑

该函数是上述问题的解法,但是结果不对,请教一下问题在哪里了?
  1. int calNumOfTag(int pointA){
  2.         int tagNum = 0;
  3.         for(int x1 = 0; x1 <= pointA; x1++){
  4.                 for(int y1 = 0; y1<=pointA; y1++){
  5.                         for(int x2 = 0; x2 <= pointA; x2++){
  6.                                 for(int y2 = 0; y2<= pointA ; y2++){
  7.                                         if((x1 == x2 && y1 == y2)|| (x1 == 0 && y1 == 0) || (x2 ==0 && y2 == 0)){
  8.                                                 continue;
  9.                                         }
  10.                                         int edge_1 = x1*x1 + y1*y1;
  11.                                         int edge_2 = x2*x2 + y2*y2;
  12.                                         int edge_3 = (x2 - x1)*(x2 - x1) + (y2 - y1)*(y2 -y1);
  13.                                         if(edge_1 + edge_2 == edge_3 || edge_1 + edge_3 == edge_2 || edge_3 + edge_2 == edge_1 ){
  14.                                                 tagNum++;
  15.                                         }
  16.                                 }
  17.                         }
  18.                 }
  19.         }
  20.         return tagNum/2;//相同点的组合会重复取到
  21. }
复制代码


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

使用道具 举报

发表于 2020-2-26 23:44:33 | 显示全部楼层
本帖最后由 major_lyu 于 2020-2-26 23:52 编辑

正确答案是多少?你也不给说一下。
我算出来时40.

  1. #include <stdio.h>
  2. int calNumOfTag(int pointA)
  3. {
  4.     int tagNum = 0;
  5.     for (int x1 = 0; x1 <= pointA; x1++)
  6.     {
  7.         for (int y1 = 0; y1 <= pointA; y1++)
  8.         {
  9.             for (int x2 = 0; x2 <= pointA; x2++)
  10.             {
  11.                 for (int y2 = 0; y2 <= pointA; y2++)
  12.                 {
  13.                     for (int x3 = 0; x3 <= pointA; x3++)
  14.                     {
  15.                         for (int y3 = 0; y3 <= pointA; y3++)
  16.                         {
  17.                             int edge_1 = (x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2);   //计算三条边长度的平方
  18.                             int edge_2 = (x1 - x3) * (x1 - x3) + (y1 - y3) * (y1 - y3);
  19.                             int edge_3 = (x3 - x2) * (x3 - x2) + (y3 - y2) * (y3 - y2);
  20.                             if (edge_1 == 0 || edge_2 == 0 || edge_3 == 0) // 任意一条边为0,即有两点重合,进行下一组选点
  21.                             {
  22.                                 continue;
  23.                             }
  24.                             if (edge_1 + edge_2 == edge_3 || edge_1 + edge_3 == edge_2 || edge_3 + edge_2 == edge_1) // 勾股定理判定
  25.                                 tagNum++;
  26.                         }
  27.                     }
  28.                 }
  29.             }
  30.         }
  31.     }
  32.     return tagNum / 6; //相同点的组合会重复取到
  33. }

  34. int main()
  35. {
  36.     printf("%d", calNumOfTag(2));

  37.     return 0;
  38. }
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2020-2-27 11:14:05 | 显示全部楼层
major_lyu 发表于 2020-2-26 23:44
正确答案是多少?你也不给说一下。
我算出来时40.
  1. /*功能函数,实现直角三角形个数的计算*/
  2. int calNumOfTag(int pointA){
  3.         int tagNum = 0;
  4.         int count = 0;
  5.         for(int x1 = 0; x1 <= pointA; x1++){
  6.                 for(int y1 = 0; y1<=pointA; y1++){
  7.                         for(int x2 = 0; x2 <= pointA; x2++){
  8.                                 for(int y2 = 0; y2<= pointA ; y2++){
  9.                                         if((x1 == x2 && y1 == y2)|| (x1 == 0 && y1 == 0) || (x2 ==0 && y2 == 0)){
  10.                                                 continue;
  11.                                         }
  12.                                         //count++;
  13.                                         //printf("符合条件的点的集合___%d____:A(%d,%d),B(%d,%d)\n",count,x1,y1,x2,y2);
  14.                                         //printf("*******\n");
  15.                                         int edge_1 = x1*x1 + y1*y1;
  16.                                         //printf("edge_1 = x1*x1 + y1*y1[%d]\n",edge_1);
  17.                                         int edge_2 = x2*x2 + y2*y2;
  18.                                         //printf("edge_2 = x2*x2 + y2*y2[%d]\n",edge_2);
  19.                                         int edge_3 = (x2 - x1)*(x2 - x1) + (y2 - y1)*(y2 -y1);
  20.                                         //printf("edge_3 = (x2 - x1)*(x2 - x1) + (y2 - y1)*(y2 -y1)[%d]\n",edge_3);
  21.                                         if(edge_1 + edge_2 == edge_3 || edge_1 + edge_3 == edge_2 || edge_3 + edge_2 == edge_1 ){
  22.                                                 tagNum++;
  23.                                                 //printf("能组成直角三角形的点是:B(%d,%d),C(%d,%d)\n",x1,y1,x2,y2);
  24.                                         }
  25.                                 }
  26.                         }
  27.                 }
  28.         }
  29.         return tagNum/2 ;
  30. }
复制代码


## 正确答案是 0<= a <= 2 时是14; 0<= a <=50时是14234
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2020-2-27 11:18:44 | 显示全部楼层
major_lyu 发表于 2020-2-26 23:44
正确答案是多少?你也不给说一下。
我算出来时40.

感谢您的解答,是我没把问题描述清楚,抱歉啊
这个链接是鱼C论坛上该问题的描述
https://fishc.com.cn/thread-74810-1-3.html
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-2-27 15:19:25 | 显示全部楼层

要求的是有一个顶点在原点的直角三角形啊!!!!!
你之前的描述是顶点坐标在0~2,我以为三个顶点都可以动呢。
  1. #include <stdio.h>
  2. int calNumOfTag(int pointA)
  3. {
  4.     int tagNum = 0;
  5.     for (int x1 = 0; x1 <= pointA; x1++)
  6.     {
  7.         for (int y1 = 0; y1 <= pointA; y1++)
  8.         {
  9.             for (int x2 = 0; x2 <= pointA; x2++)
  10.             {
  11.                 for (int y2 = 0; y2 <= pointA; y2++)
  12.                 {

  13.                     int edge_1 = x1 * x1 + y1 * y1;
  14.                     int edge_2 = x2 * x2 + y2 * y2;
  15.                     int edge_3 = (x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2);
  16.                     if (edge_1 == 0 || edge_2 == 0 || edge_3 == 0)
  17.                     {
  18.                         continue;
  19.                     }
  20.                     if (edge_1 + edge_2 == edge_3 || edge_1 + edge_3 == edge_2 || edge_3 + edge_2 == edge_1)
  21.                     {
  22.                         tagNum++;
  23.                     }
  24.                 }
  25.             }
  26.         }
  27.     }
  28.     return tagNum / 2; //相同点的组合会重复取到
  29. }

  30. int main()
  31. {
  32.     printf("%d", calNumOfTag(50));

  33.     return 0;
  34. }
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

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

本版积分规则

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

GMT+8, 2025-7-6 15:07

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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