JAVA编程:已知:s=1-1/2+1/3-1/4+…+1/(n-1)-1/n,编写程序求解n=100时的S值.(用JAVA语言编写),程序哪public class Exe45 {public static void main(String args[]){double s=1.0000;for(int i=1;i
来源:学生作业帮助网 编辑:作业帮 时间:2024/11/17 23:31:51
JAVA编程:已知:s=1-1/2+1/3-1/4+…+1/(n-1)-1/n,编写程序求解n=100时的S值.(用JAVA语言编写),程序哪public class Exe45 {public static void main(String args[]){double s=1.0000;for(int i=1;i
JAVA编程:已知:s=1-1/2+1/3-1/4+…+1/(n-1)-1/n,编写程序求解n=100时的S值.(用JAVA语言编写),程序哪
public class Exe45
{
public static void main(String args[])
{
double s=1.0000;
for(int i=1;i<=49;i++)
{
s=s+(1/(double)(2*i+1))-(1/(double)(2*i));
}
double sum=s-1/100;
System.out.println("s=1-1/2+...-1/100的和="+sum);
}
JAVA编程:已知:s=1-1/2+1/3-1/4+…+1/(n-1)-1/n,编写程序求解n=100时的S值.(用JAVA语言编写),程序哪public class Exe45 {public static void main(String args[]){double s=1.0000;for(int i=1;i
楼主,运行了下你的程序 貌似没什么问题,我的:
public void col(){
double s=1;
for(int i=2;i
public class Test {
public static void main(String args[]) {
System.out.println("s=1-1/2+...-1/100=" + count(100));
}
private static double count(int n) {
if (1 == n) {
return ...
全部展开
public class Test {
public static void main(String args[]) {
System.out.println("s=1-1/2+...-1/100=" + count(100));
}
private static double count(int n) {
if (1 == n) {
return n;
}
double result = 1 / (double) n;
if (n % 2 == 0) {
result = -result;
}
return count(n - 1) + result;
}
}
收起
套高深了