从0相加若干个数,直到总合等于100
相加的数要保持降低趋势直到等于100
比如:0+9+8.5+8.2+7.4.......................=100;
而且每次相加的数字是无规律的,而且必须保持降低趋势
请问如何实现?

解决方案 »

  1.   

    using System;
    using System.Data;
    using System.Configuration;
    using System.Collections;
    using System.Web;
    using System.Web.Security;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Web.UI.WebControls.WebParts;
    using System.Web.UI.HtmlControls;
    using System.Collections.Generic;public partial class Default7 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                String strValue = String.Empty;
                foreach (Decimal decItem in GetList())
                {
                    strValue += decItem.ToString() + "+";
                }
                strValue = strValue.Substring(0, strValue.Length - 1)+"=100";
                Response.Write(strValue);
            }
            
        }    public List<Decimal> GetList()
        {
            List<Decimal> objList = new List<decimal>();
            objList.Add(0);        Decimal decValue = 0.0m;
            while ((decValue = GetValue(objList[objList.Count - 1])) < 100.00m - GetSum(objList))
            {
                objList.Add(decValue);
            }        objList.Add(100.00m - GetSum(objList));        return objList;
        }
            public Decimal GetSum( List<Decimal> objList)
        {
            Decimal decReturnValue = 0.00m;
            if (objList != null)
            {
                foreach (Decimal decItem in objList)
                {
                    decReturnValue += decItem;
                }
            }
            return decReturnValue;
        }    public Decimal GetValue(Decimal decValue)
        {
            if (decValue == 0)
                decValue = 100.00m;
            Decimal decTemp = 0.0m;
            while ((decTemp = 100m*Convert.ToDecimal(new Random().NextDouble())) > decValue && decTemp != 0.00m) { }
            return decTemp;
        }}
      

  2.   

    每次的结果都是不一样的
    //0+65.828671569856200+34.171328430143800=100 
    //0+17.268492010081400+17.268492010081400+17.268492010081400+17.268492010081400+17.268492010081400+13.657539949593000=100 
    //0+41.254063575181200+41.254063575181200+17.491872849637600=100 
     
    如果想精确几位小数,需要Decimal.Round(Decimal,intDecimals)