php20以内的勾股数,[求助]编程求100以内的所有勾股数

27 回复

2ebef30d2f576db8dbe620d13b89a96b.gif

#2

zhaofeng2007-03-16 18:45

(一) 如果 是一个大于1的奇数, 为两个连续自然数,且有 ,则为一组勾股数.如3、4、5是一组勾股数,且有 ,

5,12, 13为一组勾股数,

7, 24, 25为一组勾股数,

(二)如果 为一组勾股数,则 也是一组勾股数,其中n为自然数

例 3, 4,5是一组勾股数,那么

6、8、10也是一组勾股数

9、12、15也是一组勾股数

9186a0bfc8435915598d9c0a11f3741c.png

#3

nuciewth2007-03-16 18:55

LS的这些条件是什么?

2ebef30d2f576db8dbe620d13b89a96b.gif

#4

高达2007-03-16 19:00

时间复杂度尽量小

有很大一部分都是 人工完成有关计算的

或 寻找独特的规律来解决问题

193b76e590c89a7c47eea276b47767d1.png

#5

I喜欢c2007-03-16 19:02

穷举法最简单...

想想其他的!

2ebef30d2f576db8dbe620d13b89a96b.gif

#6

jiangliangju2007-03-16 19:36

#include "stdio.h"

main()

{ int a,b,c;

for(a=1;a<=100;a++)

for(b=1;b<=100;b++)

for(c=1;c<=100;c++)

if(a*a+b*b==c*c&&a

printf("%d\t%d\t%d\n",a,b,c);

em02.gif

system("pause");

}

这是我的代码

15678b147fd621ec175e76ab4a2309a8.gif,有没有人能让他更简便一点,我现在想不出更好的判断方法.

46914278917d415428f969b3c454927a.gif

2ebef30d2f576db8dbe620d13b89a96b.gif

#7

zhaofeng2007-03-16 19:43

#include "math.h"

#include "string.h"

void GetPythafromanynaturalNum(int nn)

{

int m =0,n = 0;

int m_max=0,n_max=0;

int x=0,y=0,z=0;

m_max = sqrt(1.0*nn);

n_max = sqrt(1.0*nn/2);

for (n=1;n

for(m=n+1;m

{

x=m*m-n*n;

y= 2*m*n;

z=m*m+n*n;

printf("The absolute value of x=%d y=%d z=%d \n", x,y,z);

}

}

void main(void)

{

int naturalnum;

naturalnum =100;

GetPythafromanynaturalNum(naturalnum);

}

2ebef30d2f576db8dbe620d13b89a96b.gif

#8

zhaofeng2007-03-16 19:44

刚写好

我的msn:yangzhou_zf@hotmail.com

2ebef30d2f576db8dbe620d13b89a96b.gif

#9

zhaofeng2007-03-16 19:49

我不知道全不全

2ebef30d2f576db8dbe620d13b89a96b.gif

#10

jiangliangju2007-03-16 19:52

请问楼上的,你这样算出来的勾股数有33组,而我的有55组, 是不是你落了什么,还是我多了

a4d5f4c458f4f7d8f144815bcad6ceb4.png

#11

pinglideyu2007-03-16 19:56

#include

#define N 100

void main()

{

int i,j,k;

int t=0;

printf("打印%d以内所有的勾股数:\n",N);

for (i=1;i<=N;i++)

{

for (j=1;j<=N;j++)

{

for (k=1;k<=N;k++)

{

if ((i*i+j*j==k*k)&&(i

printf("%d %d %d\n",i,j,k);

}

}

}

}

2ebef30d2f576db8dbe620d13b89a96b.gif

#12

jiangliangju2007-03-16 19:58

The absolute value of x=3 y=4 z=5

The absolute value of x=8 y=6 z=10

The absolute value of x=15 y=8 z=17

The absolute value of x=24 y=10 z=26

The absolute value of x=35 y=12 z=37

The absolute value of x=48 y=14 z=50

The absolute value of x=63 y=16 z=65

The absolute value of x=80 y=18 z=82

The absolute value of x=5 y=12 z=13

The absolute value of x=12 y=16 z=20

The absolute value of x=21 y=20 z=29

The absolute value of x=32 y=24 z=40

The absolute value of x=45 y=28 z=53

The absolute value of x=60 y=32 z=68

The absolute value of x=77 y=36 z=85

The absolute value of x=7 y=24 z=25

The absolute value of x=16 y=30 z=34

The absolute value of x=27 y=36 z=45

The absolute value of x=40 y=42 z=58

The absolute value of x=55 y=48 z=73

The absolute value of x=72 y=54 z=90

The absolute value of x=9 y=40 z=41

The absolute value of x=20 y=48 z=52

The absolute value of x=33 y=56 z=65

The absolute value of x=48 y=64 z=80

The absolute value of x=65 y=72 z=97

The absolute value of x=11 y=60 z=61

The absolute value of x=24 y=70 z=74

The absolute value of x=39 y=80 z=89

The absolute value of x=56 y=90 z=106

The absolute value of x=13 y=84 z=85

The absolute value of x=28 y=96 z=100

The absolute value of x=45 y=108 z=117

33'd:\mplay.com'

请按任意键继续. . .为什么会有117呢,大于100什么意思

8f3a92fe7b9f8b4314e0d30853af843b.png

#13

neverTheSame2007-03-16 19:59

我的思路是这样的:

勾股数是满足:a的平方+b的平方=c的平方

为了保证不重复,

for(a=1;a

for(b=a;b

{再验证这三个数能不能成立}

但为了少做一些不必要的循环次数

a最大到 sqrt(2)*(NUM)/2,

for(a=1;a

在做验证这一步时:

先让一个数=sqrt(a的平方+b的平方),

再验证这个数的平方?=a的平方+b的平方;

#include

#include

#include

int check(int,int);

int main(void)

{

int number1,number2,number3;

int control=(int)(sqrt(2)*50);

for(number1=1;number1

for(number2=number1;number2<100;number2++)

{

number3=check(number1,number2);

if(number3!=0)

printf("%d %d %d \n",number1,number2,number3);

}

getch();

return 0;

}

int check(int num1,int num2)

{

int num3=0;

int sqrtData=num1*num1+num2*num2;

num3=(int)sqrt(sqrtData);

if(num3<100)

{

if(num3*num3==sqrtData)

return num3;

else

return 0;

}

else

return 0;

}

2ebef30d2f576db8dbe620d13b89a96b.gif

#14

zhaofeng2007-03-16 20:03

我在优化以下!

2ebef30d2f576db8dbe620d13b89a96b.gif

#15

lhj20052007-03-16 21:13

谁能加个计数器COUNT,计算下,100以内的勾谷数个数???

8f3a92fe7b9f8b4314e0d30853af843b.png

#16

neverTheSame2007-03-16 21:33

5 12 13

6 8 10

7 24 25

8 15 17

9 12 15

9 40 41

10 24 26

11 60 61

12 16 20

12 35 37

13 84 85

14 48 50

15 20 25

15 36 39

16 30 34

16 63 65

18 24 30

18 80 82

20 21 29

20 48 52

21 28 35

21 72 75

24 32 40

24 45 51

24 70 74

25 60 65

27 36 45

28 45 53

30 40 50

30 72 78

32 60 68

33 44 55

33 56 65

35 84 91

36 48 60

36 77 85

39 52 65

39 80 89

40 42 58

40 75 85

42 56 70

45 60 75

48 55 73

48 64 80

51 68 85

54 72 90

57 76 95

60 63 87

65 72 97

2ebef30d2f576db8dbe620d13b89a96b.gif

#17

zhaofeng2007-03-16 21:38

pinglideyu

能不能把你的55组也贴上来,我想验证一下

2ebef30d2f576db8dbe620d13b89a96b.gif

#18

喝酸奶的鱼2007-03-16 21:53

neverTheSame能不能解释一下为什么“a最大到 sqrt(2)*(NUM)/2”。

2ebef30d2f576db8dbe620d13b89a96b.gif

#19

zhaofeng2007-03-16 22:04

设定3个数 a b c

a*a +b*b= c*c

三个数是不等的

所以a>b或b>a

所以假设a=b

a*a+b*b=c*c

也就是 a*a+a*a=c*c 同理c*c =num

2*a*a =num

a=sqrt(mun/2)

2ebef30d2f576db8dbe620d13b89a96b.gif

#20

flmls2007-03-16 22:56

谁会设置一个计数器啊,

放在程序中,效果是最后计算出 一共有几个符合条件的 勾古

8f3a92fe7b9f8b4314e0d30853af843b.png

#21

neverTheSame2007-03-16 23:35

以下是引用喝酸奶的鱼在2007-3-16 21:53:22的发言:

neverTheSame能不能解释一下为什么“a最大到 sqrt(2)*(NUM)/2”。

为了确保不重复计,

后面的b要大于等于a,

也就可以说:

a2(a的平方) b2(b的平方) c2(c的平方)

a2+b2>=2*a2

c2>=a2+b2

c2>=2*a2

由于c<100

所以,100的平方大于等于 2*a2

a最大到 sqrt(2)*(NUM)/2

2ebef30d2f576db8dbe620d13b89a96b.gif

#22

renwoxing2007-03-17 10:31

#include

main()

{int a,b,c,i=0;

for(a=1;a<=100;a++)

{for(b=1;b<=100;b++)

for(c=1;c<=100;c++)

if(a*a+b*b==c*c&&a<=b&&b

{i++;

printf("a%d=%d,b%d=%d,c%d=%d\n",i,a,i,b,i,c);

}

}

}

2ebef30d2f576db8dbe620d13b89a96b.gif

#23

爱以走远2007-03-17 14:29

以下是引用I喜欢c在2007-3-16 19:02:49的发言:

穷举法最简单...

想想其他的!

说的时间要小的嘛 你都是3重循环哦 代码是简单

2ebef30d2f576db8dbe620d13b89a96b.gif

#24

爱以走远2007-03-17 14:30

引用错拉哈 不好意思

2ebef30d2f576db8dbe620d13b89a96b.gif

#25

zhanghuan_102007-03-17 14:36

6楼的还有等腰直角三角形呢!

2ebef30d2f576db8dbe620d13b89a96b.gif

#26

喝酸奶的鱼2007-03-17 20:18

以下是引用neverTheSame在2007-3-16 23:35:12的发言:

为了确保不重复计,

后面的b要大于等于a,

也就可以说:

a2(a的平方) b2(b的平方) c2(c的平方)

a2+b2>=2*a2

c2>=a2+b2

c2>=2*a2

由于c<100

所以,100的平方大于等于 2*a2

a最大到 sqrt(2)*(NUM)/2

谢谢诶 ,明白了!

87c2dc896857db0d96afa48878d6e49f.png

#27

aphase2010-05-16 00:32

虽然是旧帖

但是发现了错误

将NUM定义为10试试

只出现了3.4.5

而没有6.8.10

2ebef30d2f576db8dbe620d13b89a96b.gif

#28

lucky5635912010-05-16 07:34

I do not know

1

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值