Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/300.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# 从小组中计算百分比的程序_C#_Math - Fatal编程技术网

C# 从小组中计算百分比的程序

C# 从小组中计算百分比的程序,c#,math,C#,Math,有人能帮我编码吗。我想计算出这个分组的最终百分比 100% of 4 0% of 1 100% of 12 100% of 2 所以答案应该是 95% of 19 或者类似的 这可能吗 谢谢假设您有某种结构来保存您的百分比以及属于这些百分比的项目数,下面的代码将满足您的要求: struct Data { double Percent; int Count; } ... List<Data> items = new List<Data> (); ..

有人能帮我编码吗。我想计算出这个分组的最终百分比

100% of 4
0% of 1
100% of 12
100% of 2
所以答案应该是

95% of 19
或者类似的

这可能吗


谢谢

假设您有某种结构来保存您的百分比以及属于这些百分比的项目数,下面的代码将满足您的要求:

struct Data
{
    double Percent;
    int Count;
}

...

List<Data> items = new List<Data> ();
... // fill your list with Data instances, initialized with values

double total = 0; // running total
int totalcount = 0; // total number of items

for ( int i = 0; i < items.Count; i++ )
{
    totalcount += items[i].Count;
    total += ( items[i].Count * items[i].Percent );
}

double result = total / totalcount;
struct数据
{
百分之二;
整数计数;
}
...
列表项=新列表();
... // 用数据实例填充列表,并用值初始化
双倍合计=0;//运行总数
int totalcount=0;//项目总数
对于(int i=0;i
通常,计算y的x%的“加权平均值”的公式为:

sum(x/100 * y) / sum(y)
所以你的例子会产生效果

(1.0 * 4) + (0.0 * 1) + (1.0 * 12) + (1.0 * 2) / (4 + 1 + 12 + 2)

= 18 / 19 = ~94.7%

当然,这是可能的。但是,要回答这个问题,您需要指定数据存储的格式。顺便说一句:你试过什么?因为数学是琐碎的。目前这是一个数学问题,与编程无关,因为Daniel指出了原因。谢谢你的回答Daniel,你所说的数据格式是什么意思?我没有尝试太多,因为我不知道如何去解决它。谢谢你的答案,但我选择了@Stanley