要实现一方法:
public decimal CalculatePrice(int age)
{
decimal totalValue = 0;
if (age == 1)
   totalValue = 0;
else if (age == 2)
{
   totalvalue = 100 * decimal.Parse(2.35);
}
else if (age > 2)
{
   totalvalue = CalculatePrice(age - 1) * 13.65 + 100;//需要调用自身公式
}
return totalvalue;
}
要求:请不要用递归的方法;谢谢!

解决方案 »

  1.   

    原型公式如下:CalculatePrice (t)
      If t = 1
         CalculatePrice (t) = 0
      If t = 2
         CalculatePrice (t) = 100* 2.5) 
      If t > 2
         CalculatePrice (t) = CalculatePrice (t-1) * 13.65 + 100不能用递归的方法,请问该怎么实现,小弟先谢了!
      

  2.   

    错误1:totalValue...totalvalue...大小写分清楚...
    错误2:totalvalue = CalculatePrice(age - 1) * 13.65 + 100; 改成 totalvalue = CalculatePrice(age - 1) * 13.65m + 100; 数据类型问题...警告1:totalvalue = 100 * decimal.Parse(2.35); 改成 totalvalue = 100 * 2.35m; 优化...其他没看出什么问题...这种玩意儿递归最简单...为什么不用?
      

  3.   

    参考以下的代码,没经过调试,不过基本意思是:递归可以用循环代替。        public decimal CalculatePrice(int age)
            {
                decimal totalValue = 0;
                if (age == 1)
                    totalValue = 0;
                else if (age == 2)
                {
                    totalvalue = 100 * decimal.Parse(2.35);
                }
                else if (age > 2)
                {
                    totalvalue = 100 * decimal.Parse(2.35);
                    for (int idx = 3; idx <= age; idx++)
                    {
                        totalvalue = totalvalue * 13.65 + 100;
                    }
                }
                return totalvalue;
            }
      

  4.   

    多谢 vrhero(授人以渔还要看对象...天才=99%的汗水+1%的灵感,)
    不好意思,只想着表达意思没太注意细节。public decimal CalculatePrice(int age)
    {
        decimal totalValue = 0;
        if (age == 1)
           totalValue = 0;
        else if (age == 2)
        {
           totalValue = 100 * 2.35m;
        }
        else if (age > 2)
        {
           totalValue = CalculatePrice(age - 1) * 13.65m + 100;//需要调用自身公式
        }
        return totalValue ;
    }因为当 age > 2 时里面的算法比较复杂,这里我只是举个例子。
    如查用递归的话速度比较慢,也比较占内存,所以想用其它的方法实现。
      

  5.   

    //当age>2的时候
    public int Sum(int age)
    {
       int sum=0;
       
        for(int i=age;i<0;i--)
           {
               sum+=i * 13.65 + 100;
           }
        return sum;
      
    }
      

  6.   

    各位请注意:在age -1 的过程中,当age - 1 == 2 时会执行totalValue = 100 * 2.35m;而不是一直执行 * 13.65m + 100 
    (我想应该是这样吧)
      

  7.   

    采用非递归来计算时,主要就是拆解这句
    CalculatePrice (t) = CalculatePrice (t-1) * 13.65 + 100因为t是逐次减一,那么简单办法就如楼上所写用循环来进行判断
    不过楼上写的循环貌似优点问题
    for (int i = t; i >2 ;i--)
    {
    sum += sum  * 13.65 + 100;
    }
      

  8.   

    帮你改写了一下
        class price
        {
            public int Age = 0;
            public int getAgeOne = 0;
            public decimal getAgeTwo = 100 * 2.35m;
            public decimal GetAllValue(int age)
            {
                Age = age;
                if (Age == 1) return getAgeOne;
                if (Age == 2) return getAgeTwo;
                if (Age > 2)
                {
                    int i = Age - 2;
                    decimal c = getAgeTwo;
                    for (int k = 0; k < i; k++)
                    {
                        c = c * 13.65m + 100;
                    }
                    return c;
                }
                return 0;
            }    }
        class Test
        {
            [STAThread]
            public static void Main(string[] args)
            {
                decimal g = CalculatePrice(10);
                System.Console.WriteLine(Convert.ToString(g));
                price t = new price();
                decimal k = t.GetAllValue(10);
                System.Console.WriteLine(Convert.ToString(k));
                System.Console.ReadLine();
               
            }        public static decimal CalculatePrice(int age)
            {
                decimal totalValue = 0;
                if (age == 1)
                    totalValue = 0;
                else if (age == 2)
                {
                    totalValue = 100 * 2.35m;
                }
                else if (age > 2)
                {
                    totalValue = CalculatePrice(age - 1) * 13.65m + 100;//需要调用自身公式
                }
                return totalValue;
            }
      

  9.   

    ...再次发送代码,感觉应该可以满足lz要求,并且算是简洁吧        public decimal CalculatePrice(int age)
            {
                decimal totalValue = 0;
                if (age == 1)
                    totalValue = 0;
                else if (age == 2)
                {
                    totalvalue = 100 * decimal.Parse(2.35);
                }
                else if (age > 2)
                {
                    totalvalue = 100 * decimal.Parse(2.35);
                    for (int idx = 3; idx <= age; idx++)
                    {
                        totalvalue = totalvalue * 13.65 + 100;
                    }
                }
                return totalvalue;
            }
      

  10.   

    谢谢各位,已解决。
    to neucf(),cdsgajxlp(起名很难) 非常感谢。