区间之和

来源:互联网 发布:知行乐学教育集团 编辑:程序博客网 时间:2024/06/01 07:40

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

#include <stdio.h>int main(){    int n,l,r,i;    int a[10010],sum=0;    scanf("%d",&n);    for(i=1; i<=n; i++)    {        scanf("%d",&a[i]);    }    scanf("%d %d",&l,&r);    for(i=l; i<=r; i++)    {        sum+=a[i];    }    printf("%d\n",sum);    return 0;}
原创粉丝点击