* 13、求?1-1/3+1/5-1/7+1/9......的值。? a,求出前?50?项和值。? b,求出最后一项绝对值小于?1?的和值。?
*/
public static void main(String[] args) {
Demo1();
double fenzi = 1;
double fenmu = 1;
double sum = 0;
double num = 0;
for (int i = 1; i < 500; i++) {
sum = fenzi / fenmu;
fenzi = -fenzi;
fenmu += 2;
if (sum > -1 && sum < 1) {
num += sum;
}
}
System.out.println(num);
}
private static void Demo1() {
double fenzi = 1.0, fenmu = 1.0, sum = 0;
for (int i = 1; i < 50; i++) {
sum += fenzi / fenmu;
fenzi = -fenzi;
fenmu += 2;
}
System.out.println(sum);
}
|
|