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;
}