[HackerRank] Simple Arra Sum | A Ver Big Sum

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

61 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);    }}

关键字:java, basic, sum, solution


本文来自互联网用户投稿,文章观点仅代表作者本人,不代表本站立场,不承担相关法律责任。如若转载,请注明出处。 如若内容造成侵权/违法违规/事实不符,请点击【内容举报】进行投诉反馈!

立即
投稿

微信公众账号

微信扫一扫加关注

返回
顶部