用C++是挺方便的- -!
#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;
int main()
{
cout << "Please enter a series of numbers to calculate the average, "
"median and variance of them, followed by end-of-file: " << endl;
double x, sum = 0;
vector<double> num;
while (cin >> x) {
num.push_back(x);
sum += x;
}
// calculate average
vector<double>::size_type size = num.size(), mid = size / 2;
double average = sum / size;
// calculate median
sort(num.begin(), num.end());
double median = (size % 2 == 0) ? (num[size] + num[size-1]) / 2 : num[mid];
// calculate variance
double s = 0;
for (int i = 0; i < size; i++)
s += (num[i] - average) * (num[i] - average);
s /= size;
cout << "The average: " << average << endl << "The median: " << median
<< endl << "The variance: " << s << endl;
return 0;
}