资讯专栏INFORMATION COLUMN

[HackerRank] Simple Array Sum | A Very Big Sum

harriszh / 2800人阅读

Simple Array Sum

Problem

Given an array of N integers, can you find the sum of its elements?

Input Format

The first line contains an integer, N, denoting the size of the array.
The second line contains N space-separated integers representing the array"s elements.

Output Format

Print the sum of the array"s elements as a single integer.

Sample Input

6
1 2 3 4 10 11

Sample Output

31

Solution

import java.io.*;
import java.util.*;

public class Solution {
    public static void main(String[] args) {
        /* Enter your code here. Read input from STDIN. Print output to STDOUT. Your class should be named Solution. */
        Scanner sc = new Scanner(System.in);
        int size = sc.nextInt();        
        int sum = 0;

        while(sc.hasNextInt()){          
            sum += sc.nextInt();
        }
        System.out.print(sum);
    }
}
A Very Big Sum

Solution

import java.io.*;
import java.util.*;

public class Solution {

    public static void main(String[] args) {
        /* Enter your code here. Read input from STDIN. Print output to STDOUT. Your class should be named Solution. */
        Scanner sc = new Scanner(System.in);
        int size = sc.nextInt();        
        long sum = 0;

        while(sc.hasNextInt()){          
            sum += (long)sc.nextInt();
        }
        System.out.print(sum);
    }
}

文章版权归作者所有,未经允许请勿转载,若此文章存在违规行为,您可以联系管理员删除。

转载请注明本文地址:https://www.ucloud.cn/yun/66020.html

相关文章

  • [HackerRank] Diagonal Difference

    Problem Given a square matrix of size N x N, calculate the absolute difference between the sums of its diagonals. Input Format The first line contains a single integer, N. The next N lines denote the ...

    warmcheng 评论0 收藏0
  • 剑指offer--JavaScript版

    摘要:剑指在一个二维数组中,每一行都按照从左到右递增的顺序排序,每一列都按照从上到下递增的顺序排序。例如输入前序遍历序列和中序遍历序列,则重建二叉树并返回。其中负数用补码表示。 本文为8月牛客网《剑指 offer》刷题做得,现整理出来作为参考。虽然是算法题,但本文用 JavaScript 编写,看了《剑指 offer》以后发现很多问题处理的过程并不是最好的,所以本文仅供参考。以前全部代码 A...

    MarvinZhang 评论0 收藏0
  • 一些做着玩的题

    摘要:这是我在平时有时间的时候做的一些算法上的题目想看更新请移步这里题目描述解法这个问题当时拿到的时候是完全没有思路的,后面上网查询了一下这个题目,知道了使用斐波那契数列就能够解这道题目,,,当然百度作业帮上面也有相应的解法,套路就是题目为一 这是我在平时有时间的时候做的一些算法上的题目 想看更新请移步这里 题目: Climbing Stairs 描述 You are climbing a ...

    cheukyin 评论0 收藏0
  • Make a simple custom EventEmitter

    Thoughts Recently I have been reading the book Async Javascript about JS asynchronicity and JS event is one of the useful solutions to the problem. To get a deeper understanding of how events work, I ...

    fizz 评论0 收藏0
  • You need to know curry

    Functions are first-class citizen Functions are first-class citizen in JavaScript, as the same as other types(e.g. number, array, object). They can be used as arguments, as well as return value from o...

    BigNerdCoding 评论0 收藏0

发表评论

0条评论

harriszh

|高级讲师

TA的文章

阅读更多
最新活动
阅读需要支付1元查看
<