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. Input: The firs ...
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 And Your Code's 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 434 499 485 480 447 559 426 455 482 494 501 511 4...
截圖如下:
這是為什麼我的代碼會多輸出這麼多數據呢?我很不理解。。。。