求直角三角形三条边如果三条边加起来为1000(最好根据c++的程序来解释)

能用a = n^2 - m^2

b = 2mn

c = n^2 + m^2

a+b+c=1000
请问能用这种方法解释如何得出答案200,375,425吗
以下为c++程序,该怎么来解释怎么得到这个结论吗?
#include<iostream>
using namespace std;
int main()
{ int a,b,c,m,n;
for(n=1;n<1000;n++)
for(m=1;m<n;m++){
a=n*n-m*m;
b=2*m*n;
c=n*n+m*m;
if(a+b+c==1000)
cout<<a<<", "<<b<<", "<<c;
}
}

#include<iostream>

using namespace std;

int main()

{ int a,b,c,m,n;

  for(a=1; a<707; a++) //穷举a,b 

    for(b=a; b<708; b++)

    { c=1000-a-b; //保证a+b+c==1000 

      if(a*a+b*b==c*c) //如果a,b,c是勾股数组 

        cout<<a<<", "<<b<<", "<<c<<endl;

    }

}

这样的程序更简洁,更容易理解。

温馨提示:答案为网友推荐,仅供参考
第1个回答  2019-09-12
直角三角形只需要判断两边的平方和等于第三边就行了

#include<iostream>
using namespace std;
int main() {
int a,b,c;
for(a=1; a<1000; a++)
for(b=1; b<1000; b++)
for(c=1; c<1000; c++) {
if((a+b+c)!=1000) continue;
if(a*a+b*b==c*c)
cout<<a<<", "<<b<<", "<<c<<endl;
}
}
第2个回答  2019-09-12
写了一段程序,注释没写太多。可以参考一下。粘到这了哈~

// File: main.h
/*
* main.h
*
* Created on: 2012-2-6
* Author: Administrator
*/

#ifndef MAIN_H_
#define MAIN_H_

#include "stdio.h"

//declare the function
void initialTriangle(int* x, int* y, int* z);
int check(int x, int y, int z);
void print(int x, int y, int z);

//define return macros
#define SUCCESS 0
#define ERROR 1

#define NOT_A_TRIANGLE 0
#define RIGHT_ANGLED_TRIANGLE 1
#define COMMON_TRIANGLE 2

#define TRY_AGAIN 1
#define QUIT 0

#endif /* MAIN_H_ */

//===================================
//File: main.cpp
/*
* main.cpp
*
* Created on: 2012-2-6
* Author: Administrator
*/

#include "main.h"

int main()
{
int choice = TRY_AGAIN;

do
{
//define three edges of the triangle
int x = 0;
int y = 0;
int z = 0;

//initialize the tree edges
initialTriangle(&x, &y, &z);

//check whether it is right-angled
switch(check(x, y, z))
{
case NOT_A_TRIANGLE:
printf("This is not a triangle. \n");
break;
case COMMON_TRIANGLE:
printf("This is a common triangle. \n");
break;
case RIGHT_ANGLED_TRIANGLE:
printf("This is a right-angled triangle.\n");
print(x, y, z);
break;
}
fflush(stdout);

printf("\n");
printf("Enter\n");
printf("1 to Try again\n");
printf("0 to Quit\n");
printf("Choice> ");
fflush(stdout);

scanf("%d", &choice);
}
while (QUIT != choice);

return SUCCESS;
}

//initial three edges of the triangle
void initialTriangle(int* x, int* y, int* z)
{
printf("Please enter the length of the edges:\n");

//if a negative number is entered, try it again
while (*x <= 0)
{
printf("x> ");
fflush(stdout);
scanf("%d", x);
}

while (*y <= 0)
{
printf("y> ");
fflush(stdout);
scanf("%d", y);
}

while (*z <= 0)
{
printf("z> ");
fflush(stdout);
scanf("%d", z);
}
}

//check if it is a right-angled triangle
int check(int x, int y, int z)
{
if ((x > 0) && (y > 0) && (z > 0))
{
if ((x + y <= z)
|| (x + z <= y)
|| (y + z < x))
{
return NOT_A_TRIANGLE;
}
else
{
if ((x * x + y * y == z * z)
|| (y * y + z * z == x * x)
|| (x * x + z * z == y * y))
{
return RIGHT_ANGLED_TRIANGLE;
}
else
{
return COMMON_TRIANGLE;
}
}
}
else
{
return NOT_A_TRIANGLE;
}
}

//print the information of the triangle
void print(int x, int y, int z)
{
printf("x=%d, y=%d, z=%d\n", x, y, z);
}

以上就是所有代码了,下面说明一下:
1、main.h中定义了很多宏,主要是为了增加程序的易读性,约定返回某个数代表某个含义,不要读不下去。例如
#define RIGHT_ANGLED_TRIANGLE 1
就是用返回1表示直角三角形,在编写程序时,不用思考到底是1还是2还是3了。当然在定义这个宏时,后面写什么数都无所谓。

2、头文件一般要定义防止重复包含的宏,一般定义为:文件名的大写_H_,用以下形式包含头文件。这个在学习C语言时,可以暂时不管。
#ifdef 文件名的大写_H_
#define 文件名的大写_H_
......
#endif 文件名的大写_H_

3、定义了三个辅助函数,主要是为了让main函数线索更清晰。主要功能函数名已经表示的比较清楚。

4、其实三角形的三条边用三个整型变量表示不好,应该声明一个结构体如下
struct Triangle
{
int x;
int y;
int z;
};
然后再用它来进行运算,但昨天没把程序调过去,暂时先用三个变量代替,到时候你可以自己试一下。

5、fflush()是一个强制刷新输出缓冲区的函数。我在程序中加了很多这个函数,主要原因是printf()在编译的时候,不同的编译器实现不同。我用的MinGW GCC编译器(实际上是C++的编译器),它在实现的时候好像没有让printf()刷新输出缓冲区,即使我在最后加了个'\n'。所以要进行强制刷新。不过在VC6.0上编译应该不用了。
ps:
能导致缓冲区刷新的情况
a、强制刷新 fflush;
b、放到缓冲区到内容中包含\n \r ;
c、缓冲区已满;
d、需要从缓冲区拿东西到时候,如执行scanf。

6、还有一些代码格式标准的问题,我觉得我都遵守的挺好,像双目运算符前后要有空格啊、换行时运算符应该在行首啊、Tab都要换成四个Space啊什么的。不过要是有不好的地方,希望指正。

7、对了,还有一点忘了说。就是这个程序虽然检查了输入值的正负等问题,但是如果输入字符如:a、*、空格、汉字等,还是无法处理。鲁棒性还有待提高。