Time Limit: 1000 ms Memory Limit: 65536 KiB
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 类型范围内。
Sample Input
5
3 5 6 2 9
2 4
Sample Output
13
Hint
Source
#include <stdio.h>
#include <stdlib.h>
int main()
{
int n;
int a[10001];
int i,l,r;
int sum = 0; //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-1;i++) //注意:区间是从1开始,所以要减1;
sum+=a[i];
printf("%d\n",sum);
return 0;
}
5
3 5 6 2 9
2 4
13
Process returned 0 (0x0) execution time : 3.518 s
Press any key to continue.