要实现一方法:
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;
}
要求:请不要用递归的方法;谢谢!
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;
}
要求:请不要用递归的方法;谢谢!
解决方案 »
- 求助,关于string.IndexOf(char value)
- 字体缩放
- 求模糊查询程序实例
- 请教一个listview的colunmsclick事件排序问题?
- 结合工厂与三层要这样设计?
- 怎么判断远程remoting service url有没打开,是否有效
- 难道这个问题没人能解决吗?还是没有好心人愿意帮助我?发了2遍都没人理!55555555
- 水晶报表打包问题
- 请问在VISTA中对MSINFO32.EXE应该添加什么参数可以得到我想要的那一部分的内容呢??
- c#的switch和vb的select的比较,c#难道没有好的方法吗?
- 关于门襟的刷卡或者指纹管理系统
- C#中" 并非所有的代码路径都返回值"请问这是什么错误啊? 急求,请大虾帮不弟看看
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: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; 优化...其他没看出什么问题...这种玩意儿递归最简单...为什么不用?
{
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;
}
不好意思,只想着表达意思没太注意细节。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 时里面的算法比较复杂,这里我只是举个例子。
如查用递归的话速度比较慢,也比较占内存,所以想用其它的方法实现。
public int Sum(int age)
{
int sum=0;
for(int i=age;i<0;i--)
{
sum+=i * 13.65 + 100;
}
return sum;
}
(我想应该是这样吧)
CalculatePrice (t) = CalculatePrice (t-1) * 13.65 + 100因为t是逐次减一,那么简单办法就如楼上所写用循环来进行判断
不过楼上写的循环貌似优点问题
for (int i = t; i >2 ;i--)
{
sum += sum * 13.65 + 100;
}
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;
}
{
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;
}
to neucf(),cdsgajxlp(起名很难) 非常感谢。