﻿ [Mean of range in array] - 鸿网互联

# [Mean of range in array]

Given an array ofnintegers andqqueries. Write a program to print floor value of mean in rangeltorfor each query in a new line. Examples:Input : arr[] = {1, 2, 3, 4, 5} q = 3 0 2 1 3 0 4Output : 2 3 3Here for 0 to 2 (1 + 2 + 3) / 3 = 2Input

Given an array of n integers and q queries. Write a program to print floor value of mean in range l to r for each query in a new line.

```Examples:
Input : arr[] = {1, 2, 3, 4, 5}
q = 3
0 2
1 3
0 4
Output : 2
3
3
Here for 0 to 2 (1 + 2 + 3) / 3 = 2

Input : arr[] = {6, 7, 8, 10}
q = 2
0 3
1 2
Output : 7
7
```

Input:

The first line consists of an integer T i.e number of test cases.

The first line of each test case consists of two integer n and q.

The next line consists of n spaced integers. The next line consists of 2*k spaced integers each pair representing the range.

Output:
Print the mean(floor value) in given range for each query in new line.

Constraints:
1<=T<=100
1<=q<n<=1000
1<=a[i]<=1000

Example:
Input:
1
5 3
1 2 3 4 5
0 2 1 3 0 4

Output:
2 3 3

```#include <stdio.h>

int main() {
int num,i;//测试用例
scanf("%d\n",&num);

for(i=0;i<num;i++)
{
int N,M,j;

scanf("%d",&N);
scanf("%d",&M);
int *Arr=(int *)malloc(sizeof(int)*N);
int *Brr=(int *)malloc(sizeof(int)*2*M);
for(j=0;j<N;j++)//依次输入数组元素
scanf("%d",&Arr[j]);

for(j=0;j<2*M;j++)//依次输入"测试对"的值
scanf("%d",&Brr[j]);

for(j=0;j<M;j++)//循环求解平均值
{
int avg=0,sum=0,k;
for(k=Brr[2*j];k<=Brr[2*j+1];k++)
{
sum=sum+Arr[k];//求和
}
avg=sum/(Brr[2*j+1]-Brr[2*j]+1);//平均值=和/元素个数。
printf("%d ",avg);
}
}
return 0;
}
```

然后提交的时候，出现的问题是：

```Wrong Answer. !!!Wrong Answer
Possibly your code doesn't work correctly for multiple test-cases (TCs).
The first test case where your code failed:

Input:
42 30
335 501 170 725 479 359 963 465 706 146 282 828 962 492 996 943 828 437 392 605 903 154 293 383 422 717 719 896 448 727 772 539 870 913 668 300 36 895 704 812 323 334
2 33 21 25 22 29 8 11 28 39 3 7 29 39 19 22 10 11 14 36 34 36 10 38 10 28 20 33 9 14 10 20 12 21 20 33 15 23 4 18 24 38 10 26 16 29 15 18 14 25 34 35 24 32 35 39 3 35 6 19

Its Correct output is:
610 393 575 490 640 598 657 488 555 607 334 624 615 625 617 697 671 625 548 618 641 609 566 650 589 484 678 549 616 646

610 393 575 490 640 598 657 488 555 607 334 624 615 625 617 697 671 625 548 618 641 609 566 650 589 484 678 549 616 646 434 499 485 480 447 559 426 455 482 494 501 511 4...
```

截图如下：

++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++问题已经解决++++++分割线+++++问题已经解决++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

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

int main()
{
int num,i;//测试用例
scanf("%d\n",&num);

for(i=0;i<num;i++)
{
int N,M,j;

scanf("%d",&N);
scanf("%d",&M);
int *Arr=(int *)malloc(sizeof(int)*N);
int *Brr=(int *)malloc(sizeof(int)*2*M);
for(j=0;j<N;j++)//依次输入数组元素
scanf("%d",&Arr[j]);

for(j=0;j<2*M;j++)//依次输入"测试对"的值
scanf("%d",&Brr[j]);

for(j=0;j<M;j++)//循环求解平均值
{
int avg=0,sum=0,k;
for(k=Brr[2*j];k<=Brr[2*j+1];k++)
{
sum=sum+Arr[k];//求和
}
avg=sum/(Brr[2*j+1]-Brr[2*j]+1);//平均值=和/元素个数。
printf("%d ",avg);
}
printf("\n");
}
return 0;
}
```

还有一种更简单的方案，如下：就是使用变量进行计算和，然后求平均值。相比之前的程序，节省了存储空间。

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

int main()
{
int num,i;
scanf("%d",&num);
for(i=0;i<num;i++)
{
int N,M,j;//N是数组元素个数，M是测试对。j循环变量
scanf("%d",&N);
scanf("%d",&M);
int *Arr=(int *)malloc(sizeof(int)*N);
for(j=0;j<N;j++)
scanf("%d",&Arr[j]);
int left, right,k;//left和right是范围变量，k是循环变量,sum是当前和。
int *result=(int *)malloc(sizeof(int)*M);//存放结果。
for(j=0;j<M;j++)
{
scanf("%d",&left);
scanf("%d",&right);
int sum=0;
for(k=left;k<=right;k++)
{
sum=sum+Arr[k];
}
result[j]=sum/(right-left+1);
}
for(j=0;j<M;j++)
printf("%d ",result[j]);
}
return 0;
}
```

如有疑问，请留言。

<