HDOJ(HDU) 2162 Add ‘em(求和)

Problem Description

Write a program to determine the summation of several sets of integers.

Input

The input file will consist of up to 250 sets of integers, where each set contains at most 100 integers and the integer values will be between –16000 and + 16000. Each set of numbers is started with the number of integers in the set, n. The next n input lines will each contain one integer of the set. You should stop processing when the size of the set, n, is<= 0.

Output

A single line of output should be generated for each set of integers. The line should have format shown below.

Sample Input

4

-1

3

1

1

2

19

17

5

-645

952

-1488

-5456

-9342

-1

Sample Output

Sum of #1 is 4

Sum of #2 is 36

Sum of #3 is -15979

简单题,就是求和。

import java.util.Scanner;

public class Main{

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int t=0;
        while(sc.hasNext()){
            int n =sc.nextInt();
            if(n==-1){
                break;
            }
            long  sum=0;
            for(int i=0;i<n;i++){
                long a = sc.nextLong();
                sum+=a;
            }
            System.out.println("Sum of #"+(++t)+" is "+sum);
        }
    }
}
时间: 2024-10-09 02:14:59

HDOJ(HDU) 2162 Add ‘em(求和)的相关文章

HDU 2162 Add ‘em

http://acm.hdu.edu.cn/showproblem.php?pid=2162 Problem Description Write a program to determine the summation of several sets of integers. Input The input file will consist of up to 250 sets of integers, where each set contains at most 100 integers a

HDOJ(HDU) 2304 Electrical Outlets(求和、、)

Problem Description Roy has just moved into a new apartment. Well, actually the apartment itself is not very new, even dating back to the days before people had electricity in their houses. Because of this, Roy's apartment has only one single wall ou

HDOJ/HDU 1250 Hat&#39;s Fibonacci(大数~斐波拉契)

Problem Description A Fibonacci sequence is calculated by adding the previous two members the sequence, with the first two members being both 1. F(1) = 1, F(2) = 1, F(3) = 1,F(4) = 1, F(n>4) = F(n - 1) + F(n-2) + F(n-3) + F(n-4) Your task is to take

HDOJ/HDU 2140 Michael Scofield&#39;s letter(字符转换~)

Problem Description I believe many people are the fans of prison break. How clever Michael is!! In order that the message won't be found by FBI easily, he usually send code letters to Sara by a paper crane. Hence, the paper crane is Michael in the he

HDU 2162(注意初始化位置!)

Add ‘em Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 4144    Accepted Submission(s): 2976 Problem Description Write a program to determine the summation of several sets of integers. Input The

HDOJ/HDU 1161 Eddy&#39;s mistakes(大写字母转换成小写字母)

Problem Description Eddy usually writes articles ,but he likes mixing the English letter uses, for example "computer science" is written frequently "coMpUtEr scIeNce" by him, this mistakes lets Eddy's English teacher be extremely disco

HDOJ(HDU) 2156 分数矩阵(嗯、求和)

Problem Description 我们定义如下矩阵: 1/1 1/2 1/3 1/2 1/1 1/2 1/3 1/2 1/1 矩阵对角线上的元素始终是1/1,对角线两边分数的分母逐个递增. 请求出这个矩阵的总和. Input 每行给定整数N (N<50000),表示矩阵为 N*N.当N为0时,输入结束. Output 输出答案,保留2位小数. Sample Input 1 2 3 4 0 Sample Output 1.00 3.00 5.67 8.83 简单题 不打表会超时.... 还可

HDU 5358 First One 求和(序列求和,优化)

题意:给定一个含n个元素的序列,求下式子的结果.S(i,j)表示为seq[i...j]之和.注:对于log20可视为1.数据量n<=105. 思路:即使能够在O(1)的时间内求得任意S,也是需要O(n*n)来求和的. 对于这种题,一般就是研究式子,看有什么办法可以减少复杂度. 对于这个式子,S最大也不会超过longlong,确切计算,小于234.那么log的范围这么小,如果能够知道分别有多少个的话,那就快多了.可以看得出对于同一个i,log的结果是线性的,从1到34逐步递增的.那很好办,对于每个

HDOJ(HDU) 2401 Baskets of Gold Coins(数列、)

Problem Description You are given N baskets of gold coins. The baskets are numbered from 1 to N. In all except one of the baskets, each gold coin weighs w grams. In the one exceptional basket, each gold coin weighs w-d grams. A wizard appears on the