C# 浮动';平均值';差百分之一

C# 浮动';平均值';差百分之一,c#,C#,我正在在线解决这个问题: 它的基本思想是,我应该找到一个数字的所有部分,并显示范围,平均值和中值。该程序可以很好地处理大多数数字,但当遇到一些数字时,平均值并不准确 以下是我遇到问题的一些数字: 43——预期:平均值:202904.65但实际值:202904.60 36——预期:平均值:26832.81但实际:平均值:26832.80 41——预期:平均值:113720.82但实际:平均值:113720.80 这是因为我使用浮点数来存储我的数字吗?如果是这样,我应该使用什么数据类型来代替?

我正在在线解决这个问题:

它的基本思想是,我应该找到一个数字的所有部分,并显示范围,平均值和中值。该程序可以很好地处理大多数数字,但当遇到一些数字时,平均值并不准确

以下是我遇到问题的一些数字:

  • 43——预期:平均值:202904.65但实际值:202904.60

  • 36——预期:平均值:26832.81但实际:平均值:26832.80

  • 41——预期:平均值:113720.82但实际:平均值:113720.80

这是因为我使用浮点数来存储我的数字吗?如果是这样,我应该使用什么数据类型来代替?

这是我的代码(你可以将它直接粘贴到我链接的网站上,它会给你和我一样的错误)

使用系统;
使用System.Collections.Generic;
使用System.Collections.Specialized;
使用System.Linq;
公共类IntPart
{
公共静态列表listOfLists=新列表();
public static List lastPartion=new List();//获取最后一个分区
公共静态字符串部分(长n)
{
控制台写入线(n);
lastPartion.Clear();
listofList.Clear();
列表结果=新列表();
分区((int)n);
//消除在字符串开头有额外空间的blip,并
foreach(最后一部分中的var循环)
添加(cycle.Split(“”).ToList());
//将循环转换为列表,并将字符串列表转换为双精度
for(int i=0;ilong.Parse(x)).ToList();
}                
返回removeAndSort(结果);
}
//递归分割算法
公共静态无效分区(int n)
{
分区(n,n,“”);
}
公共静态无效分区(int n、int max、字符串前缀)
{
如果(n==0)
{
添加(前缀);
返回;
}
对于(int i=Math.Min(max,n);i>=1;i--)
分区(n-i,i,前缀+“”+i);
}
公共静态字符串removeAndSort(列表列表)
{
列表结果=新列表();
字符串resultString=“”;
//找到产品
foreach(列表中的var列表)
{
长积=1;
for(int i=0;i
此错误是由计算
浮点值的方式引起的。它们以基数2计算,而不是以基数10计算。当切换不同的基数时,有些数字不能用十进制表示

著名的是,
1/3
不能写成以10为基数的十进制,但在以12为基数的情况下,
1/3
0.4
(而
1/2
0.6

基数2无法表示十分之一
0.1
最终被
0b00110011
重复,或者诸如此类


C#中的
decimal以十为基数计算,而不是以二为基数。这使得它更准确,更符合金钱的需要,但在某种程度上速度较慢

没关系,将浮点改为十进制,数字就可以了。不过,任何让代码更好的建议都很好。事实上,你的代码格式非常好,特别是这是你在这个网站上的第一个问题。很多新来者甚至都不格式化他们的代码,更不用说使用子弹了!我唯一的评论是,
listOfLists
可以使用更好的名称来描述它的功能,可能是
partions
?,而微软的C注释惯例是做
//这个
而不是
//这个
。只是让它更容易阅读,并且几乎所有东西都可以使用
PascalCase
,除了局部变量和参数之外,它们都可以得到
camelCase
using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Linq;

public class IntPart
{
    public static List<List<string>> listOfLists = new List<List<string>>();
    public static List<string> lastPartion = new List<string>(); //get the last partion

    public static string Part(long n)
    {
        Console.WriteLine(n);
         lastPartion.Clear();
         listOfLists.Clear();

        List<List<long>> result = new List<List<long>>();
        partition((int)n);

        //gets rid of blip where there's an extra space at the start of the string and 
        foreach (var cycle in lastPartion)
            listOfLists.Add(cycle.Split(' ').ToList());

        //converts the cycles to a list and converts string list to double
        for (int i = 0; i < listOfLists.Count; i++)
        {
            listOfLists[i].RemoveAt(0);
            result.Add(listOfLists[i].Select(x => long.Parse(x)).ToList());
        }                

        return removeAndSort(result);
    }

    //partioning algorithom with recursion 
    public static void partition(int n)
    {
        partition(n, n, "");
    }
    public static void partition(int n, int max, string prefix)
    {
        if (n == 0)
        {
            lastPartion.Add(prefix);
            return;
        }

        for (int i = Math.Min(max, n); i >= 1; i--)
            partition(n - i, i, prefix + " " + i);
    }

    public static string removeAndSort(List<List<long>> listOfLists)
    {
        List<long> result = new List<long>();
        string resultString = "";

        //find the products
        foreach (var list in listOfLists)
        {
            long product = 1;

            for (int i = 0; i < list.Count; i++)
                product *= list[i];

            result.Add(product);
        }

        //removes the duplicates
        result = result.Distinct().ToList(); //returns a copy of the list without any duplicates in the previous list
        result.Sort();

        int range = (int)(result.Max() - result.Min());
        float avg = (float)result.Sum() / (float)result.Count;
        float median = 0f;

        if (result.Count % 2 == 0)
            median = (float)Math.Round((double)((result[(result.Count / 2) - 1] + result[(result.Count / 2)])) / 2, 3);
        else
            median = result[result.Count / 2]; //odd

        return "Range: " + range + " Average: " + avg.ToString("0.00") + " Median: " + median.ToString("0.00");
    }

}