USACO DEC08 Problem 'hayexp' Analysis

by Rob Kolstad

This is a fairly simple straightforward programming problem.

Make an array of bales and read in the n values for the bales.

Then, read in the start/end indices and run a counter through the proper bales to sum their counts.

Print the answer and loop.

Here's my solution:

#include <stdio.h>
#include <stdlib.h>

int bales[500+1];
main () {
    FILE *fin = fopen ("hayexp.in", "r");
    FILE *fout = fopen ("hayexp.out", "w");
    int n, q, i, j, s, e, sum;
    fscanf (fin, "%d %d", &n, &q);
    for (i = 0; i < n; i++) 
	fscanf (fin, "%d", &bales[i+1]);
    for (i = 0; i < q; i++) {
	fscanf (fin, "%d %d", &s, &e);
	sum = 0;
	for (j = s; j <= e; j++)
	    sum += bales[j];
	fprintf(fout, "%d\n", sum);
    }
    exit (0);
}