SDUT-2565 区间之和

来源:互联网 发布:php 去除数组重复值 编辑:程序博客网 时间:2024/05/29 19:41

区间之和

Time Limit: 1000MS Memory Limit: 65536KB
Submit Statistic Discuss

Problem Description

 

给定一个由 n 个整数组成的序列A1,A2,……, An 和两个整数L,R,你的任务是写一个程序来计算序列号在[L,R](这是一个闭区间) 这段位置区间内所有数的总和。

 

Input

 

输入只有一组测试数据:

测试数据的第一行为一个整数 n (1< n < 10000);

第二行为 n 个 int 类型的整数;

第三行为两个整数 L,R(0 < L < R <= n)。

Output

 

输出序列号在区间[L,R]内所有数的和,数据保证和在 int 类型范围内。

Example Input

53 1 2 9 91 3

Example Output

6

Code

#include <stdio.h>int main(){    int i,n,a[10000],L,R,sum=0;    scanf("%d",&n);    for(i=0; i<n; i++)    {        scanf("%d",&a[i]);    }    scanf("%d %d",&L,&R);    for(i=L-1; i<R; i++)    {        sum+=a[i];    }    printf("%d",sum);    return 0;}