Sum Calculation Between Given Ranges
unknown
c_cpp
13 days ago
872 B
2
Indexable
Never
#include <stdio.h> #include <stdlib.h> int main(void) { int i, j, n, s; char e[1000], a[1000][2], d[1000]; scanf("%d", &n); for(i = 0;i<n;i++) { scanf("%d", &e[i]); } scanf("%d", &s); for(i = 0;i<s;i++) { for(j = 0;j<2;j++) { scanf("%d", &a[i][j]); } } for(i = 0;i<s;i++)//計算左右界限之差 { d[i] = a[i][1] - a[i][0]; if(d[i] == 0)//左右界線相等 { printf("%d\n", e[a[i][0]-1]); } else if(d[i] != 0)//左右界線不相等 { int sum = 0; for(j = a[i][0];j<=a[i][1];j++)//初始值為左界線,加到右界線 { sum += e[j-1]; } printf("%d\n", sum); } } return 0; }
Leave a Comment