Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 用cpp求四分位数_C++_Arrays_Algorithm_Math_Statistics - Fatal编程技术网

C++ 用cpp求四分位数

C++ 用cpp求四分位数,c++,arrays,algorithm,math,statistics,C++,Arrays,Algorithm,Math,Statistics,问题: 找出一组的3个四分位数 第一行包含一个整数n,表示数组中的元素数。 第二行包含n个描述数组元素的空格分隔整数 那么什么是四分位数?有3个四分位数,第二个是中位数,是集合的中间元素,如果n%2==0是中间2除以2的和,第一个和第三个,我们得到集合的前半部分和后半部分,我们找到这些集合的中间点,基本上,这是完整的信息() 所以我想用hackerrank提交我的代码,但我似乎找不到我的错误,测试用例通过了,我做了测试,一切都正常。但是我似乎找不到我的错误,你能帮我找到它吗,但一般来说。。如果一

问题:

找出一组的3个四分位数

第一行包含一个整数n,表示数组中的元素数。 第二行包含n个描述数组元素的空格分隔整数

那么什么是四分位数?有3个四分位数,第二个是中位数,是集合的中间元素,如果n%2==0是中间2除以2的和,第一个和第三个,我们得到集合的前半部分和后半部分,我们找到这些集合的中间点,基本上,这是完整的信息()

所以我想用hackerrank提交我的代码,但我似乎找不到我的错误,测试用例通过了,我做了测试,一切都正常。但是我似乎找不到我的错误,你能帮我找到它吗,但一般来说。。如果一切正常,但某些输出不正常,那么调试此类问题的方法是什么?单元测试能帮我吗?这是我的密码:

#include <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>

// A utility function to swap two elements
void swap(int* a, int* b)
{
    int t = *a;
    *a = *b;
    *b = t;
}

/* This function takes last element as pivot, places
   the pivot element at its correct position in sorted
    array, and places all smaller (smaller than pivot)
   to left of pivot and all greater elements to right
   of pivot */
int partition (int arr[], int low, int high)
{
    int pivot = arr[high];    // pivot
    int i = (low - 1);  // Index of smaller element

    for (int j = low; j <= high- 1; j++)
    {
        // If current element is smaller than or
        // equal to pivot
        if (arr[j] <= pivot)
        {
            i++;    // increment index of smaller element
            swap(&arr[i], &arr[j]);
        }
    }
    swap(&arr[i + 1], &arr[high]);
    return (i + 1);
}

/* The main function that implements QuickSort
 arr[] --> Array to be sorted,
  low  --> Starting index,
  high  --> Ending index */
void quickSort(int arr[], int low, int high)
{
    if (low < high)
    {
        /* pi is partitioning index, arr[p] is now
           at right place */
        int pi = partition(arr, low, high);

        // Separately sort elements before
        // partition and after partition
        quickSort(arr, low, pi - 1);
        quickSort(arr, pi + 1, high);
    }
}

int getQuartil(int arr[],int len){
    int q=0;
    if(len%2){
        q=arr[len/2]; 
    }else{
        q=(arr[len/2]+arr[len/2-1])/2;

    }
    return q;
}



int main() {
    int n;
    std::cin >> n;
    int arr[n];
    for(int i =0; i < n; i++){
        std::cin >> arr[i];
    }

    quickSort(arr,0,n);
    int q1=getQuartil(arr,n/2);
    int q2=getQuartil(arr, n);
    int q3=getQuartil(arr, (n+n/2+1));    
    std::cout << q1 << std::endl;    
    std::cout << q2 << std::endl;    
    std::cout << q3 << std::endl; 
    return 0;
}
#包括
#包括
#包括
#包括
#包括
//交换两个元素的实用函数
无效交换(int*a,int*b)
{
int t=*a;
*a=*b;
*b=t;
}
/*此函数以最后一个元素为轴,放置
轴元素在其正确的位置排序
数组,并放置所有较小的(小于轴)
轴的左侧和所有较大元素的右侧
轴心*/
整数分区(整数arr[],整数低,整数高)
{
int pivot=arr[high];//pivot
int i=(低-1);//较小元素的索引
对于(int j=低;j起始指数,
高-->结束索引*/
无效快速排序(整数arr[],整数低,整数高)
{
如果(低<高)
{
/*pi是分区索引,arr[p]现在是
在正确的地方*/
int pi=分区(arr、低、高);
//在之前分别对元素进行排序
//分区和分区后
快速排序(arr、低、pi-1);
快速排序(arr、pi+1、高);
}
}
int getQuartil(int arr[],int len){
int q=0;
如果(len%2){
q=arr[len/2];
}否则{
q=(arr[len/2]+arr[len/2-1])/2;
}
返回q;
}
int main(){
int n;
标准:cin>>n;
int-arr[n];
对于(int i=0;i>arr[i];
}
快速排序(arr,0,n);
int q1=四分之一(arr,n/2);
int q2=四分之一(arr,n);
int q3=getQuartil(arr,(n+n/2+1));

我不能谢谢你,这实际上是一个非常好的答案,我为此感谢你,但是…我已经做了所有的事情(除了橡皮鸭部分),我被卡住了,我自己做了两天,我不知道怎么了…你的“中位数”2和3是2,但它应该是2.5