编写程序求和 S=1+(1+3)+(1+3+5+……+(2N-1))
1个回答
展开全部
//java写的
import java.util.Scanner;
public class SumTest {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("输入n:");
int n = sc.nextInt();
int sum = 0;
for(int i = 1;i <= n;++i){
sum +=fn(i);
}
System.out.println("和是:"+sum);
}
public static int fn(int n){
int x = 2*n-1;
int sum = 0;
while(x>0){
sum += x;
x -= 2;
}
return sum;
}
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询