-
Notifications
You must be signed in to change notification settings - Fork 7
/
Program.cs
86 lines (84 loc) · 2.36 KB
/
Program.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
//按照要求统计数字,但是中位数有可能是两个不同数的平均值,要特殊讨论。
using System;
namespace StatisticsFromALargeSample
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Hello World!");
}
static double[] SampleStats(int[] count)
{
double[] res = new double[5];
for (int i = 0; i < count.Length; i++)
{
if (count[i] != 0)
{
res[0] = i;
break;
}
}
for (int i = count.Length-1; i >= 0; i--)
{
if (count[i] != 0)
{
res[1] = i;
break;
}
}
double sum = 0;
int totalCount = 0;
int mode = count[0];
for (int i = 0; i < count.Length; i++)
{
if (count[i] != 0)
{
sum += i * count[i];
totalCount += count[i];
mode = Math.Max(mode, count[i]);
}
}
res[2] = sum / totalCount;
res[3] = FindMedian(totalCount, count);
for (int i = 0; i < count.Length; i++)
{
if (count[i] == mode)
{
res[4] = i;
break;
}
}
return res;
}
static double FindMedian(int totalCount, int[] count)
{
int kthNum = totalCount / 2;
double res = 0;
if (totalCount % 2 == 0)
{
int tem = 0;
for (int i = 0; i < count.Length; i++)
{
tem += count[i];
if (tem == kthNum)
return (double) (i * 2 + 1) / 2;
else if(tem > kthNum)
return i;
}
}
else
{
kthNum++;
int tem = 0;
for (int i = 0; i < count.Length; i++)
{
tem += count[i];
if (tem >= kthNum)
return i;
}
}
return res;
}
}
}