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

 我来答
blue_memoery
2008-12-30
知道答主
回答量:4
采纳率:0%
帮助的人:0
展开全部
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;

}
}
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
安逸且懂事灬风光5496
2008-12-31 · TA获得超过126个赞
知道答主
回答量:51
采纳率:0%
帮助的人:22.8万
展开全部
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;
}
}
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
felly822
2008-12-30 · TA获得超过265个赞
知道小有建树答主
回答量:231
采纳率:0%
帮助的人:272万
展开全部
/**
*
* @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);
}

}
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
最瑜伽
2008-12-30 · TA获得超过645个赞
知道小有建树答主
回答量:788
采纳率:0%
帮助的人:222万
展开全部
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);
}
}
本回答被网友采纳
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
匿名用户
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;
}
}
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
收起 更多回答(5)
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式