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); } }
文章版權(quán)歸作者所有,未經(jīng)允許請(qǐng)勿轉(zhuǎn)載,若此文章存在違規(guī)行為,您可以聯(lián)系管理員刪除。
轉(zhuǎn)載請(qǐng)注明本文地址:http://systransis.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 ...
摘要:劍指在一個(gè)二維數(shù)組中,每一行都按照從左到右遞增的順序排序,每一列都按照從上到下遞增的順序排序。例如輸入前序遍歷序列和中序遍歷序列,則重建二叉樹(shù)并返回。其中負(fù)數(shù)用補(bǔ)碼表示。 本文為8月牛客網(wǎng)《劍指 offer》刷題做得,現(xiàn)整理出來(lái)作為參考。雖然是算法題,但本文用 JavaScript 編寫(xiě),看了《劍指 offer》以后發(fā)現(xiàn)很多問(wèn)題處理的過(guò)程并不是最好的,所以本文僅供參考。以前全部代碼 A...
摘要:這是我在平時(shí)有時(shí)間的時(shí)候做的一些算法上的題目想看更新請(qǐng)移步這里題目描述解法這個(gè)問(wèn)題當(dāng)時(shí)拿到的時(shí)候是完全沒(méi)有思路的,后面上網(wǎng)查詢了一下這個(gè)題目,知道了使用斐波那契數(shù)列就能夠解這道題目,,,當(dāng)然百度作業(yè)幫上面也有相應(yīng)的解法,套路就是題目為一 這是我在平時(shí)有時(shí)間的時(shí)候做的一些算法上的題目 想看更新請(qǐng)移步這里 題目: Climbing Stairs 描述 You are climbing 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...
閱讀 1695·2019-08-30 15:54
閱讀 3346·2019-08-26 17:15
閱讀 3536·2019-08-26 13:49
閱讀 2589·2019-08-26 13:38
閱讀 2301·2019-08-26 12:08
閱讀 3065·2019-08-26 10:41
閱讀 1379·2019-08-26 10:24
閱讀 3387·2019-08-23 18:35