I have written below the code for calculating sum of array element
#include<stdio.h>
#define max 10
int main(){
int arr[max]={214,542,455,145,1654,655,455,452,845,254};
int sum=0,i;
for(i=0;i<10;i++)
sum=sum+arr[i];
printf("%d",sum);
return 0;
}
上面的代码给出了所需的结果,但对于大型数组,执行时间会更多。所以我想知道是否有其他有效的方法来计算非常大的数组(例如 max=100001 和元素)的总和是 arr[i]<=10^9。 提前致谢。
最佳答案
我认为你可以使用bigint来解决你的问题。
使用libgmp http://gmplib.org/
关于计算数组元素的总和,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26893059/