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
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 ...
摘要:剑指在一个二维数组中,每一行都按照从左到右递增的顺序排序,每一列都按照从上到下递增的顺序排序。例如输入前序遍历序列和中序遍历序列,则重建二叉树并返回。其中负数用补码表示。 本文为8月牛客网《剑指 offer》刷题做得,现整理出来作为参考。虽然是算法题,但本文用 JavaScript 编写,看了《剑指 offer》以后发现很多问题处理的过程并不是最好的,所以本文仅供参考。以前全部代码 A...
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 ...
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...
阅读 1681·2019-08-30 15:54
阅读 3331·2019-08-26 17:15
阅读 3521·2019-08-26 13:49
阅读 2581·2019-08-26 13:38
阅读 2290·2019-08-26 12:08
阅读 3034·2019-08-26 10:41
阅读 1368·2019-08-26 10:24
阅读 3375·2019-08-23 18:35