区间之和

来源:互联网 发布:mac虚拟机和双系统 编辑:程序博客网 时间:2024/06/05 14:33

区间之和

Time Limit: 1000MS Memory Limit: 65536KB

Submit Statistic

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

5
3 1 2 9 9
1 3

Example Output

6

Hint

Author

#include<stdio.h>#include<stdlib.h>int main(){    int n,i,s=0,l,r;    scanf("%d",&n);    int a[n+9];    for(i = 1;i <= n;i++)//注意,数组下标从1开始,而不是从0开始;    {        scanf("%d",&a[i]);    }    scanf("%d %d",&l,&r);    for(i = l;i <= r;i++)    {        s = s + a[i];    }    printf("%d\n",s);    return 0;}