c语言实验6 Problem B: 寻求勾股数

Description

满足x2+y2=z2的正整数x,y和z称为一组勾股数(pythagorean triple),又称为毕达哥拉斯三元数组。现在要求你编程求指定范围内的勾股数。

Input

输入若干对数a和b,每对数占一行。0<a,b<10000,分别为指定范围的最小值和最大值。

Output

输出为多行,与上述输入一一对应。

每行输出首先输出用例编号“case i",其中i为行号,从1开始计数。

如果指定范围内有勾股数,则按照x从小到大的顺序输出每组勾股数,每组勾股数要求x<y<z。勾股数的组与组之间用一个分号(";")隔开。

如果指定范围内没有勾股数,则输出”No pythagorean triple“。

Sample Input

1 10
6 9

Sample Output

case 1:3,4,5;6,8,10
case 2:No pythagorean triple

HINT

Append Code

 

&&

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
 int main()
{
    int tp = 1;
    int a,b;
    while(scanf("%d%d",&a,&b)!= EOF)
    {
        printf("case %d:",tp);
        int i,j,gs = 0;//每个循环重新输入gs
        for(i = a;i <= b;i++)
        for(j = i+1;j <= b;j++)
        {
            int c = sqrt(i*i+j*j); //不用第三个循环,大量减少了循环次数【Az】
            if(c <= b)
            {
                if(c*c == i*i+j*j)
                {
                    if(gs != 0)
                        printf(";");
                    printf("%d,%d,%d",i,j,c);
                    gs++;
                }
            }
        }
        if(gs == 0)
            printf("No pythagorean triple");
        printf("\n");
        tp++;
    }
    return 0;
}


 

  • 2
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值