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); } }
文章版權歸作者所有,未經允許請勿轉載,若此文章存在違規行為,您可以聯系管理員刪除。
轉載請注明本文地址:http://specialneedsforspecialkids.com/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...
閱讀 1682·2019-08-30 15:54
閱讀 3332·2019-08-26 17:15
閱讀 3522·2019-08-26 13:49
閱讀 2582·2019-08-26 13:38
閱讀 2291·2019-08-26 12:08
閱讀 3035·2019-08-26 10:41
閱讀 1369·2019-08-26 10:24
閱讀 3376·2019-08-23 18:35