日期:2014-05-17  浏览次数:20457 次

输入10个数字,返回正态分布的均数跟标准差
求方法

比如 10 15 12 16 18 12 14 21 23 26 28 24 25 返回 20 10差不多这个意思 数据可能不正确

------解决方案--------------------
C# code


using System;
using System.Collections.Generic;
using System.Text;

namespace AppMath
{
public class Maths
{
/// <summary>
/// 求出数据平均值,并保留三位小数
/// </summary>
/// <param name="Valist">数据集合</param>
/// <returns></returns>
public double average(List<double> Valist)
{
double sum = 0;
foreach (double d in Valist)
{
sum = sum + d;
}
double revl = System.Math.Round(sum / Valist.Count, 3);
return revl;
}
/// <summary>
/// 求数据集合标准差
/// </summary>
/// <param name="ValList"></param>
/// <returns></returns>
public double stdev(List<double> ValList)
{
double avg = average(ValList);
double sumstdev = 0;
foreach (double d in ValList)
{
sumstdev = sumstdev + (d - avg) * (d - avg);
}
double stdeval = System.Math.Sqrt(sumstdev);
return System.Math.Round(stdeval, 3);
}
/// <summary>
/// 相关系数,要求两个集合数量必须相同
/// </summary>
/// <param name="array1">数组一</param>
/// <param name="array2">数组二</param>
/// <returns></returns>
public double correl(List<double> array1, List<double> array2)
{
//数组一
double avg1 = average(array1);
double stdev1 = stdev(array1);
//数组二
double avg2 = average(array2);
double stdev2 = stdev(array2);

double sum = 0;
for (int i = 0; i < array1.Count && i < array2.Count; i++)
{
sum = sum + ((array1[i] - avg1) / stdev1) * ((array2[i] - avg2) / stdev2);
}
return Math.Round(sum, 3);
}
 
}
}