[HDU] [2015] Sum of Even Numbers

Problem Description

Loading...

Problem Analysis

For this problem, I first built an array of arithmetic progression
Then I used a variable to control how many numbers are averaged each time
I remember that when I first started writing it, I misunderstood the problem…
And then I struggled with it for quite a while

cpp
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
#include<stdio.h>
int main() {
	int i,j,k,n,m,x[120],sum;
	while(scanf("%d %d",&n,&m)!=EOF) {
		for(i=1,x[0]=2; i<n; i++)
			x[i]=x[i-1]+2;
		for(i=0; i<n; i=i+m) {
			for(j=i,k=0,sum=0; j<i+m; j++) {
				sum=sum+x[j];
				k++;
				if(j+1==n)
					j=i+m;
			}
			printf("%d",sum/k);
			if(i!=n-k)
				printf(" ");
		}
		printf("\n");
	}
	return 0;
}
This post is licensed under CC BY-NC-SA 4.0. If it helped, or if you have anything to say, the comment box below is open.
Page views —
Unique visitors —

Loading...