A Very Big Sum | HackerRank

该博客主要介绍了一个编程挑战,要求计算包含极大整数的数组元素之和。在C语言中,由于32位整数范围限制,可能需要使用`long long`类型来存储和防止溢出。示例代码展示了一个读取数组并计算总和的方法,最后返回数组所有元素的和。
摘要由CSDN通过智能技术生成

5 more points to get your next star!
Rank: 1519153|Points: 95/100
Problem Solving
Problem
Submissions
Leaderboard
Discussions
Editorial
In this challenge, you are required to calculate and print the sum of the elements in an array, keeping in mind that some of those integers may be quite large.
Function Description
Complete the aVeryBigSum function in the editor below. It must return the sum of all array elements.
aVeryBigSum has the following parameter(s):
int ar[n]: an array of integers .
Return
long: the sum of all array elements
Input Format
The first line of the input consists of an integer .
The next line contains space-separated integers contained in the array.
Output Format
Return the integer sum of the elements in the array.
Constraints

Sample Input
5
1000000001 1000000002 1000000003 1000000004 1000000005
Output
5000000015
Note:
The range of the 32-bit integer is .
When we add several integer values, the resulting sum might exceed the above range. You might need to use long int C/C++/Java to store such sums.
呵呵😊

#include<stdio.h>
int main ()
{
    int n ;
    long long sum = 0 ;
    scanf ("%d",&n);
    long long ar[n] ;
    for ( int i = 0 ; i < n ; i ++ )
    {
        scanf ("%lld",&ar[i]);
    }
    for ( int j = 0 ; j < n ; j ++ )
    {
        sum += ar[j] ;
    }
    printf ("%lld\n",sum);
    return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值