这是为什么我的代码会多输出这么多数据呢,我

作者: 网络资讯  发布:2019-09-25

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.

[Mean of range in array],rangearray

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 2k 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("%dn",&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...

  截图如下:

图片 1

这是为何自身的代码会多输出这么许多据吧?笔者很不知情。。。。

难题早已化解 分界线 难点一度缓和

近日晚已了然了本身事先的不当。

那个顺序,在Code Blocks里面的出口是健康的,截图如下:

图片 2

科学的出口是因为自个儿在输入案例中只输入了一个,程序猿自个儿能够调节格式。

而是怎么在geeksforgeeks就能够出错呢?

是因为当它评判的时候,是将出口放在二个文件之中,然后通过对照本人的次第的出口文件和它科学的文件,倘诺一致就通过,假使不雷同,便是wrong answer!!!。何况它的评判应该是多少个测验用例,每一个测验用例是因此换行进行剪切的。小编的前后相继中未有在每贰个测验用例截止时候经过换行进行分割。,所以,扩张了每二个测量试验用例截至时候的换行,就缓和了这几个标题。

以此标题自身不怎么精晓了有些OJ的评议方法。多加演练才是。

当今的代码更新如下:

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

int main()
{
    int num,i;//测试用例
    scanf("%dn",&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;
}

  如有疑问,请留言。

of range in array],rangearray Given an array ofnintegers andqqueries. Write a program to print floor value of mean in rangeltorfor each query in a new line. Examples:Input...

                                                                                                                                                              Frequent values

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
Time Limit: 2000MS   Memory Limit: 65536K
Total Submissions: 18520   Accepted: 6687

Input:

Description

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

You are given a sequence of n integers a1 , a2 , ... , an in non-decreasing order. In addition to that, you are given several queries consisting of indices i and j (1 ≤ i ≤ j ≤ n). For each query, determine the most frequent value among the integers ai , ... , aj.

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

Input

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

The input consists of several test cases. Each test case starts with a line containing two integers n and q (1 ≤ n, q ≤ 100000). The next line contains n integers a1 , ... , an (-100000 ≤ ai ≤ 100000, for each i ∈ {1, ..., n}) separated by spaces. You can assume that for each i ∈ {1, ..., n-1}: ai ≤ ai 1. The following q lines contain one query each, consisting of two integers i and j (1 ≤ i ≤ j ≤ n), which indicate the boundary indices for the
query.

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

The last test case is followed by a line containing a single 0.

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

Output

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

For each query, print one line with one integer: The number of occurrences of the most frequent value within the given range.

Output:
2 3 3

Sample Input

本文由巴黎人游戏官网发布于网络资讯,转载请注明出处:这是为什么我的代码会多输出这么多数据呢,我

关键词: