项目需求
1)金额10000元 ,每3年固定递增5%,共计算18年
2)金额10000元 ,每3年递增5%,接着3年递增7% 接着递增9% 。以此类推,计算18年
3) 金额10000元,第一年递增5% 第2年递增7%,第三年递增9%,以此类推,共计算18年
有没最简单的写法。只有23分,晕
1)金额10000元 ,每3年固定递增5%,共计算18年
2)金额10000元 ,每3年递增5%,接着3年递增7% 接着递增9% 。以此类推,计算18年
3) 金额10000元,第一年递增5% 第2年递增7%,第三年递增9%,以此类推,共计算18年
有没最简单的写法。只有23分,晕
年均增长率=报告期/基期^1/N-1,其中:1/N为开N次方,N为报告期与基期间隔的年限,如2006年-2009年,N=3,报告期是2009年的数据,基期为2006年的数据.
using System.Collections.Generic;
using System.Linq;
using System.Text;namespace KEVIN.Test
{
class Program
{
static void Main(string[] args)
{
double s = Sum(5);
Console.WriteLine(s); Console.Read();
} public static double Sum(int count)
{
double sum = 0;
for (int i = 0; i < count; i++)
{
sum += FixedFive(i);
}
return sum;
} public static double FixedFive(double pos)
{
double i = 1.05;
if (pos == 1 || pos == 0 ) {
return 10000;
}
if (pos%3 == 0)
{
return FixedFive(pos - 1) * i;
}
double res = FixedFive(pos - 1) ;
return res;
} }
}
double ini = 10000; double d1 = ini * Math.Pow(1.05, 6);//初始值乘以1.05的6次方.
double d2 = ini;
for (int i = 0; i < 6; i++)
{
d2 = d2 * (1.05 + 0.02 * i);
} double d3 = ini;
for (int i = 0; i < 18; i++)
{
d3 = d3 * (1.05 + 0.02 * i);
}
d1,d2,d3.三种情况
using System.Collections.Generic;
using System.Linq;
using System.Text;namespace KEVIN.Test
{
class Program
{
static void Main(string[] args)
{
double s = SumFixed(18);
Console.WriteLine(s); s = SumSetp(18);
Console.WriteLine(s); s = SumSetpPreYear(18);
Console.WriteLine(s);
Console.Read();
} /// <summary>
/// 每年递增 5% 7% 9%
/// </summary>
/// <param name="count"></param>
/// <returns></returns>
public static double SumSetpPreYear(int count)
{
double sum = 0;
for (int i = 0; i < count; i++)
{
sum += SetpUpPreYear(i);
}
return sum;
} public static double SetpUpPreYear(double pos)
{
double i = 1.05;
if (pos == 1 || pos == 0)
{
return 10000;
}
double res = SetpUpPreYear(pos - 1) * (i + 0.02 * (pos - 2));
return res;
}
/// <summary>
/// 每三年递增 5% 7% 9%
/// </summary>
/// <param name="count"></param>
/// <returns></returns>
public static double SumSetp(int count)
{
double sum = 0;
for (int i = 0; i < count; i++)
{
sum += SetpUp(i);
}
return sum;
}
public static double SetpUp(double pos)
{
double i = 1.05;
if (pos == 1 || pos == 0 ) {
return 10000;
}
if (pos%3 == 0)
{
return SetpUp(pos - 1) * (i + 0.02 * (pos / 3 - 1));
}
double res = SetpUp(pos - 1);
return res;
} /// <summary>
/// 每三年固定递增 5%
/// </summary>
/// <param name="count"></param>
/// <returns></returns>
public static double SumFixed(int count)
{
double sum = 0;
for (int i = 0; i < count; i++)
{
sum += FixedFive(i);
}
return sum;
} public static double FixedFive(double pos)
{
double i = 1.05;
if (pos == 1 || pos == 0)
{
return 10000;
}
if (pos % 3 == 0)
{
return FixedFive(pos - 1) * i;
}
double res = FixedFive(pos - 1);
return res;
} }
}
private double getRealNumber(int year, double money)
{
//float defaultLV = 0.001F;
computeMoney(year, ref money);
return money;
} private void computeMoney(int year, ref double money)
{
if (year / 3 > 0)
{
money = money * (1 + 0.05);
computeMoney(year - 3,ref money);
}
else
{
return;
}
}
没有仔细想
忘记利率会变了该下
private double getRealNumber(int year, double money)
{
//float defaultLV = 0.001F; computeMoney(year, ref money,0.05);
return money;
} private void computeMoney(int year, ref double money,double lv)
{
if (year / 3 > 0)
{
money = money * (1 + lv);
computeMoney(year - 3,ref money,lv+0.02);
}
else
{
return;
}
}