用JAVA编写求s=1+(1+2)+(1+2+3)+…+(1+2+3+…+n)的值

如题所述

第1个回答  2008-12-30
import java.io.InputStream;
import java.util.Scanner;

class Qiuhe
{
public static void main (String args[])
{
int j,result=0;
Scanner read=new Scanner(System.in);
System.out.println("请输入n的值 ");
int n=read.nextInt();
for(j=1;j<=n;j++)
{
result=result+add(j);
}
System.out.println("the result is "+result);
}
static int add(int a)
{
int t=0,i;
for(i=0;i<=a;i++)
{
t=t+i;
}
return t;

}
}
第2个回答  2008-12-31
public class And {

public static void main(String[] args){
int sum=0 ;
int n=5;//要输入的N值,你也可以用SCANNER类由控制台输入
for(int i =1;i<=n;i++){
sum +=and(i);
}
System.out.println("sum=" + sum);
}

static int and(int n){
int sum =0;
for(int i=1;i<=n;i++){
sum +=i;
}
return sum;
}
}
第3个回答  2008-12-30
/**
*
* @author Felly
* @date 2008年12月30日20:37:42
*/
public class Beans {

public static void main(String d[]) {
System.out.println(sum(3));
}

public static int sumN(int n){

return (1+n)*n/2;
}

public static int sum(int n){
if(n==1) return 1;
else
return sumN(n)+sum(n-1);
}

}
第4个回答  2008-12-30
class Sum{
public static void main(String args[]){
int sum = 0;
int n = 4;
for(int i = 1;i <= n; i++){
for(int j = i;j <= n;j++){
sum += i;
}
}
System.out.println(sum);
}
}本回答被网友采纳
第5个回答  2008-12-30
public class N {
public static void main(String[] args) {
int n = 100;
long sum=sum(n);
System.out.println(sum);
}

private static long sum(int max){
if(max>0)return haha(max)+sum(max-1);
return max;
}

private static long haha(int i) {
return (1+i)*i/2;
}
}

c语言s=1+(1+2)+(1+2+3)+…+(1+2+3+……+n)的值先编写一个求1+2+...
int sum,n;sum=0;printf("请输入n:\\n");scanf("%d",&n);for(int i=1;i<=n;i++)sum=sum+sumn(i);printf("%d",sum);}

...n值,求s=1+(1+2)+(1+2+3)+…+(1+2+3+…+n)的值。
int sum = 0, n;puts("enter n:");scanf("%d", &n);for (int i=1; i<=n; ++i){ for (int j=1; j<=i; ++j){ sum += j;} } printf("result: %d\\n", sum);return 0;}

用VB写设计程序代码,求:s=1+(1+2)+(1+2+3)+??+(1+2+3+??+n)的值。
Function sumD(n As Integer) As Single s = 1 i = 1 Do i = i + 1 s = s + 1 \/ i Loop Until i >= n sumD = s End Function Function sumF(n As Integer) As Single s = 0 For i = 1 To n s = s + 1 \/ i Next i sumF = s End Function Private Sub Form_...

求S=1+(1+2)+(1+2+3)+···+(1+2+3+···+n)级数和的java程序算法_百 ...
public class Test { public static void main(String args[]){ int n=3,s=0,s_i; \/\/n是你要求多少项,s_i是每一项的和 for(int i=0;i<=n;i++){ s_i=i*(i+1)\/2; \/\/累加求和公式 s+=s_i;} System.out.println(s); \/\/n取的是3,答案为10,你也可以设置n为...

用Java编写一个程序,计算s=1+(1+2)+(1+2+3)+…+(1+2+3+4+5+…+100...
public class Sum { public static void main(String[] args) { int sum = 0;for(int i = 1; i <= 100; i++){ int sumI = 0;\/\/calc (1+2+3) for 3 for(int j = 1; j <= i; j++){ sumI = sumI + j;} sum = sum + sumI;} System.out.println("1+(1+2)+(1...

编写一个程序,输入正整数n,计算s=1+(1+2)+(1+2+3)+...+(1+2+3+...
include <stdio.h> int fun(int n);void main(){ int i,n,sum=0;printf("请输入n:");\/\/输入数字,如10 scanf("%d",&n);for (i=1;i<=n;i++){ sum+=fun(i);} printf("sum=%d\\n",sum);} int fun(int n)\/\/计算和 { if(n==1)return 1;return n+fun(n-1);} ...

编程求1+(1+2)+(1+2+3)+……+(1+2+3…… +n), n的值由键盘输入。要求使...
include<stdio.h> int main(){ int i,n,m=0,s=0;printf("Please input the number:\\n");scanf("%d",&n);printf("n=%d\\n",n);for(i=1;i<n+1;i++){ m+=i;s+=m;} printf("s=%d\\n",s);return 0;}

4,利用For循环求S=1+(1+2)+(1+2+3)+...+(1+2+3+...+n)的值.提示:本题...
include <iostream> include <windows.h> int main(int argc, char* argv[]){ SetConsoleOutputCP(936);\/\/这是我的机器问题,你可以不用去管 long LInput(0);std::cin>>LInput;long LTemp(0);long LSum(0);for (long loopfor(0);loopfor<=LInput;++loopfor){ for (long loopIn(0);...

1+(1+2)+(1+2+3)+…+(1+2+...+n)等于多少
S=1+(1+2)+(1+2+3)+...+(1+2+3+...+n)=1*(1+1)\/2+2*(1+2)\/2+3*(1+3)\/2+...+n*(1+n)\/2 =1\/2*{(1^2+2^2+3^2+...+n^2)+(1+2+3+...+n)} =1\/2*{n(n+1)(2n+1)\/6+n(n+1)\/2} =1\/2*n(n+1)(2n+1+3)\/6 =n(n+1...

编程求1+(1+2)+(1+2+3)+……+(1+2+3…… +n), n的值由键盘输入。要求使...
s = s + e;ans = ans + s;} ③ 根据数学公式优化,复杂度 O(1)令 a1 = 1, a2 = 1 + 2, a3 = 1+2+3 ... aN = 1+2+3...+n 根据等差数列前n项和公式可知, aN 的通项公式为 aN = (N^2 + N)\/2 题目所求即为 aN的前N项和 容易观察,前N项和为 (1^2 + 2^...

相似回答