题解 | #数列求和#

数列求和

https://www.nowcoder.com/practice/6893484c04c24bfca1f606b1823a468d

public class Main {
    public static void main(String[] args) {

        //write your code here........
        //定义一个空数组,int类型长度不够,要用long,数组长度为10
        long arr[] = new long[10];
        //用for循环生成数组
        int n = 10;
        for(int i=0;i<n;i++){
            long a = (long)Math.pow(10,i+1);
            //java中次方要用math函数
            arr[i] = (a-1);
            //System.out.println(arr[i]);
            
        }
        //用tostring输出表示数组
        //System.out.println(Arrays.toString(arr));
        //循环遍历数组并求和
        long sum =0;
        for(int k=0;k<arr.length;k++){
            sum += arr[k];
        }
        System.out.println(sum);
    }
}

全部评论

相关推荐

评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务