java 5+55+555+5555+...求和

来源:百度知道 编辑:UC知道 时间:2024/06/27 04:53:26
编程题:
求5+55+555……的前十项的和
用java编程,各位大哥大姐帮帮忙,谢拉...

//用迭代的方式
//不管你是10个,还是多少个那样的数字相加 修改long ceng=..的赋值就行了

public class F {

static long a;

public static void main(String[] args) {
long ceng = 10;//10个那样的数字相加
a = ceng;
diedai(ceng, 0);
}

public static void diedai(long n, long sum) {
sum = sum + (n * 5) * (long) (Math.pow(10, a - n));
n--;
if (n > 0) {
diedai(n, sum);
}
if (n == 0) {
System.out.println(sum);
}
}
}

int ss=0;
for(int i=0;i<10;i++){
string s=null;
for(int j=0;j<10-i;j++){
s+=5;
}
int a=Integer.parseInt(s);
ss+=a;
}

这是我的思路
不知道能不能帮到你

public class Test{

public static void main(String[] args){

long j=5;
long sum=0;
for(int i=0;i<10;i++){
sum+=j;
j=Long.parseLong(j+"5");
}
System.out.println(sum);
}
}
-------<