当横纵坐标都位于0<= a <= 2内时,一共有多少个直角三角形?
本帖最后由 guoquanli 于 2020-2-26 17:28 编辑该函数是上述问题的解法,但是结果不对,请教一下问题在哪里了?
int calNumOfTag(int pointA){
int tagNum = 0;
for(int x1 = 0; x1 <= pointA; x1++){
for(int y1 = 0; y1<=pointA; y1++){
for(int x2 = 0; x2 <= pointA; x2++){
for(int y2 = 0; y2<= pointA ; y2++){
if((x1 == x2 && y1 == y2)|| (x1 == 0 && y1 == 0) || (x2 ==0 && y2 == 0)){
continue;
}
int edge_1 = x1*x1 + y1*y1;
int edge_2 = x2*x2 + y2*y2;
int edge_3 = (x2 - x1)*(x2 - x1) + (y2 - y1)*(y2 -y1);
if(edge_1 + edge_2 == edge_3 || edge_1 + edge_3 == edge_2 || edge_3 + edge_2 == edge_1 ){
tagNum++;
}
}
}
}
}
return tagNum/2;//相同点的组合会重复取到
}
本帖最后由 major_lyu 于 2020-2-26 23:52 编辑
正确答案是多少?你也不给说一下。
我算出来时40.
#include <stdio.h>
int calNumOfTag(int pointA)
{
int tagNum = 0;
for (int x1 = 0; x1 <= pointA; x1++)
{
for (int y1 = 0; y1 <= pointA; y1++)
{
for (int x2 = 0; x2 <= pointA; x2++)
{
for (int y2 = 0; y2 <= pointA; y2++)
{
for (int x3 = 0; x3 <= pointA; x3++)
{
for (int y3 = 0; y3 <= pointA; y3++)
{
int edge_1 = (x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2); //计算三条边长度的平方
int edge_2 = (x1 - x3) * (x1 - x3) + (y1 - y3) * (y1 - y3);
int edge_3 = (x3 - x2) * (x3 - x2) + (y3 - y2) * (y3 - y2);
if (edge_1 == 0 || edge_2 == 0 || edge_3 == 0) // 任意一条边为0,即有两点重合,进行下一组选点
{
continue;
}
if (edge_1 + edge_2 == edge_3 || edge_1 + edge_3 == edge_2 || edge_3 + edge_2 == edge_1) // 勾股定理判定
tagNum++;
}
}
}
}
}
}
return tagNum / 6; //相同点的组合会重复取到
}
int main()
{
printf("%d", calNumOfTag(2));
return 0;
} major_lyu 发表于 2020-2-26 23:44
正确答案是多少?你也不给说一下。
我算出来时40.
/*功能函数,实现直角三角形个数的计算*/
int calNumOfTag(int pointA){
int tagNum = 0;
int count = 0;
for(int x1 = 0; x1 <= pointA; x1++){
for(int y1 = 0; y1<=pointA; y1++){
for(int x2 = 0; x2 <= pointA; x2++){
for(int y2 = 0; y2<= pointA ; y2++){
if((x1 == x2 && y1 == y2)|| (x1 == 0 && y1 == 0) || (x2 ==0 && y2 == 0)){
continue;
}
//count++;
//printf("符合条件的点的集合___%d____:A(%d,%d),B(%d,%d)\n",count,x1,y1,x2,y2);
//printf("*******\n");
int edge_1 = x1*x1 + y1*y1;
//printf("edge_1 = x1*x1 + y1*y1[%d]\n",edge_1);
int edge_2 = x2*x2 + y2*y2;
//printf("edge_2 = x2*x2 + y2*y2[%d]\n",edge_2);
int edge_3 = (x2 - x1)*(x2 - x1) + (y2 - y1)*(y2 -y1);
//printf("edge_3 = (x2 - x1)*(x2 - x1) + (y2 - y1)*(y2 -y1)[%d]\n",edge_3);
if(edge_1 + edge_2 == edge_3 || edge_1 + edge_3 == edge_2 || edge_3 + edge_2 == edge_1 ){
tagNum++;
//printf("能组成直角三角形的点是:B(%d,%d),C(%d,%d)\n",x1,y1,x2,y2);
}
}
}
}
}
return tagNum/2 ;
}
## 正确答案是 0<= a <= 2 时是14; 0<= a <=50时是14234 major_lyu 发表于 2020-2-26 23:44
正确答案是多少?你也不给说一下。
我算出来时40.
感谢您的解答,是我没把问题描述清楚,抱歉啊
这个链接是鱼C论坛上该问题的描述
https://fishc.com.cn/thread-74810-1-3.html guoquanli 发表于 2020-2-27 11:14
## 正确答案是 0
要求的是有一个顶点在原点的直角三角形啊!!!!!
你之前的描述是顶点坐标在0~2,我以为三个顶点都可以动呢。
#include <stdio.h>
int calNumOfTag(int pointA)
{
int tagNum = 0;
for (int x1 = 0; x1 <= pointA; x1++)
{
for (int y1 = 0; y1 <= pointA; y1++)
{
for (int x2 = 0; x2 <= pointA; x2++)
{
for (int y2 = 0; y2 <= pointA; y2++)
{
int edge_1 = x1 * x1 + y1 * y1;
int edge_2 = x2 * x2 + y2 * y2;
int edge_3 = (x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2);
if (edge_1 == 0 || edge_2 == 0 || edge_3 == 0)
{
continue;
}
if (edge_1 + edge_2 == edge_3 || edge_1 + edge_3 == edge_2 || edge_3 + edge_2 == edge_1)
{
tagNum++;
}
}
}
}
}
return tagNum / 2; //相同点的组合会重复取到
}
int main()
{
printf("%d", calNumOfTag(50));
return 0;
}
页:
[1]