整理题目
假设a、b、c是三角形的三条边,当三条边符合勾股定理时,即,a2+b2=c2 ,为直角三角形。若a、b、c均为小于等于50的整数,求能够组成直角三角形的所有组合。请显示边的各种可能组合情况,显示总的组合数量。注意:
(a=3, b=4, c=5)
(a=4, b=3, c=5)
(a=5, b=4, c=3)
等情况只能算1种组合。(参考答案:20种)
三层for循环不断遍历
public class *{
public static void main(String[] args) {
int a, b, c, number = 0;
for (a = 1; a <= 50; a++) {
for (b = 1; b <= 50; b++) {
for (c = 1; c <= 50; c++) {
if (a*a+b*b==c*c && a>b) {//a+b用来去除重复
System.out.println("a="+a+"b="+ b+"c="+c);
number++;//计数
}
}
}
}
System.out.println("总数" + number);
}
}
计算π的近似值。公式如下:

直到累加项的绝对值小于10-4为止(即求和的各项的绝对值均大于等于10-4)。
public class Test14 {
public static void main(String[] args) {
double ret = 1;7
int n = 1;
double sum = 0;
int sign = -1;
while (Math.abs(ret) > 1E-9) {//
sign = -sign;
ret = 1 / (sign * n * (1.0));
sum += ret;
n += 2;
System.out.println(Math.abs(ret));
}
System.out.println("π的值" + sum * 4);
}
}
oraclepeixun

浙公网安备 33010602011771号