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...

  截图如下:

这是为什么我的代码会多输出这么多数据呢?我很不理解。。。。

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

现在已经理解了我之前的错误。

这个程序,在Code Blocks里面的输出是正常的,截图如下:

正确的输出是因为我在输入案例中只输入了一个,程序员自己可以控制格式。

但是为什么在geeksforgeeks就会出错呢?

是因为当它评判的时候,是将输出放在一个文件里面,然后通过对比我的程序的输出文件和它正确的文件,如果一致就通过,如果不一致,就是wrong answer!!!。并且它的评判应该是多个测试用例,每一个测试用例是通过换行进行分割的。我的程序中没有在每一个测试用例结束时候通过换行进行分割。,所以,增加了每一个测试用例结束时候的换行,就解决了这个问题。

这个题目我稍微理解了一些OJ的评判方法。多加练习才是。

现在的代码更新如下:

#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;
}

  如有疑问,请留言。

[Mean of range in array]的更多相关文章

  1. 【VBA】利用Range声明Array(一维/二维)

    [说明] B2开始到B?(中间不能有空格),定义一维数组Arr_approver() Dim R_sh As Worksheet Set R_sh = ThisWorkbook.Sheets(&quo ...

  2. 转 python range 用法

    详细记录python的range()函数用法   使用python的人都知道range()函数很方便,今天再用到他的时候发现了很多以前看到过但是忘记的细节.这里记录一下range(),复习下list的 ...

  3. 详细记录python的range()函数用法

    使用python的人都知道range()函数很方便,今天再用到他的时候发现了很多以前看到过但是忘记的细节.这里记录一下range(),复习下list的slide,最后分析一个好玩儿的冒泡程序. 这里记 ...

  4. range,shuffle,str_shuffle

    print_r(range(1,20)); 输出,range产生 Array( [0] => 1 [1] => 2 [2] => 3 [3] => 4 [4] => 5 ...

  5. iOS 判断数组array中是否包含元素a,取出a在array中的下标+数组方法详解

    目前找到来4个解决办法,第三个尤为简单方便 NSArray * arr = @["]; //是否包含 "]) { NSInteger index = [arr indexOfObj ...

  6. python 内置函数range和xrange

    range range 是一种类型(type),它是一个数字的序列,而且是不可变的,通常用在for循环中. class range(stop)class range(start, stop [, st ...

  7. python中的range与xrange

    range 也是一种类型(type),它是一个数字的序列(s sequence of numbers),而且是不可变的,通常用在for循环中. class range(stop) class rang ...

  8. (转)python的range()函数用法

    使用python的人都知道range()函数很方便,今天再用到他的时候发现了很多以前看到过但是忘记的细节.这里记录一下range(),复习下list的slide,最后分析一个好玩儿的冒泡程序. 转自: ...

  9. range() 函数详解 python

    使用python的人都知道range()函数很方便,今天再用到他的时候发现了很多以前看到过但是忘记的细节.这里记录一下range(),复习下list的slide,最后分析一个好玩儿的冒泡程序. 这里记 ...

随机推荐

  1. propertychange 不起作用

    兼容性问题.将$("#systemLogSettings td[name='nMaxFileSize'] input").on("input propertychange ...

  2. java基础回顾(2)

    java中只有两种类型:基础类型.引用类型 8中基本类型:byte  short int long float double char boolean,其中byte类型取值范围[-2^7~2^7-1] ...

  3. openstack Keystone验证服务集群

    #Keystone验证服务群集 openstack pike 部署 目录汇总 http://www.cnblogs.com/elvi/p/7613861.html ##.Keystone验证服务集群 ...

  4. Retrofit网络请求库应用01

    PS:什么是Retrofit? 在官方文档中有这样一句话--A type-safe HTTP client for Android and Java(一个类型安全的http client库),具体的话 ...

  5. 深入理解php内核 编写扩展 I:介绍PHP和Zend

    内容: 编写扩展I -  PHP和Zend起步 原文:http://devzone.zend.com/public/view/tag/Extension Part I: Introduction to ...

  6. POST和GET有什么区别?

    1. GET主要用于从服务器查询数据,POST用于向服务器提交数据 2. GET通过URL传递数据,POST通过http请求体传递数据 3. GET传输数据量有限制,不能大于2kb,POST传递的数据 ...

  7. Spring JDBC(二)SimpleJdbcInsert

    上一篇写了关于jdbcTemplate的一些基本使用,这一篇来聊聊SimpleJdbcInsert SimpleJdbcInsert是springjdbc提供的一个简化插入操作的类,下面来看一下常用的 ...

  8. 团队合作-如何避免JS冲突

    解决JS冲突的演化过程 1.用匿名函数将脚本包裹起来,可以有效控制全局变量,避免冲突隐患 (function(){})(): 2.定义一个全局作用域的变量str,可以帮助我们在不同匿名函数间通信 严格 ...

  9. JavaWeb 之文件的上传下载

    又到了每周更新博客的时候了,每看到自己发布的随笔阅读量上涨的时候就特别开心,我也会尽自己的努力提高自己的水平,总结出通俗易读的学习笔记,还望大家能多多支持!!! ------------------- ...

  10. java学习笔记之集合家族1

    集合 集合介绍: 由于数组中存放对象,对对象操作起来不方便.java中有一类容器,专门用来存储对象. 集合与数组的区别: 1.数组的长度固定的,而集合长度时可变的 2.数组只能储存同一类型的元素,而且 ...