TOP101题解 | BM48#数据流中的中位数#
数据流中的中位数
https://www.nowcoder.com/practice/9be0172896bd43948f8a32fb954e1be1
#include <stdlib.h> int dataflu[1000]; int dataflu_len = 0; int compar(const void* q1, const void* q2) { return (*(int*)q1 - *(int*)q2); } /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * * @param num int整型 * @return 无 */ void Insert(int num ) { // write code here dataflu[dataflu_len++] = num; } /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * * @param 无 * @return double浮点型 */ double GetMedian() { // write code here qsort(dataflu, dataflu_len, sizeof(dataflu[0]), compar); double result = 0; if(dataflu_len % 2 == 0) { result = (dataflu[dataflu_len/2 - 1] + dataflu[dataflu_len/2])/2.0; } else { result = dataflu[dataflu_len/2]; } return result; }#TOP101#
TOP101-BM系列 文章被收录于专栏
系列的题解