需求:
1)金额10000元,每3年固定递增5%,共计算18年
2)金额10000元,每3年递增5%,接着3年递增7% 接着递增9% 。以此类推,计算18年
3) 金额10000元,第一年递增5% 第2年递增7%,第三年递增9%,以此类推,共计算18年
解决:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace KEVIN.Test
{
class SetpTest
{
public static void Test() {
int year =18;
double s = SumFixed(year);
Console.WriteLine(s);
s = SumSetp(year);
Console.WriteLine(s);
s = SumSetpPreYear(year);
Console.WriteLine(s);
//s = FixedFive(year);
//Console.WriteLine(s);
//s = SetpUp(year);
//Console.WriteLine(s);
//s = SetpUpPreYear(year);
//Console.WriteLine(s);
year = 2;
s = FixedFive1(year);
Console.WriteLine(s);
}
/// <summary>
/// 每?年¨º递ÌY增? 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 <= 0)
return 0;
if (pos == 1)
{
return 10000;
}
double res = SetpUpPreYear(pos - 1) * (i + 0.02 * (pos - 2)) ;
return res;
}
/// <summary>
/// 每?三¨y年¨º递ÌY增? 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 <= 0)
return 0;
if (pos == 1 || pos == 2)
{
return 10000;
}
if (pos % 3 == 0)
{
return SetpUp(pos - 1) * (i + 0.02 * (pos / 3 - 1));
}
double res = SetpUp(pos - 1);
return res;
}
/// <summary>
/// 每?三¨y年¨º固¨¬定¡§递ÌY增? 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 <= 0)
return 0;
if (pos == 1 || pos == 2)
{
return 10000;
}
if (pos % 3 == 0)
{
return FixedFive(pos - 1) * i ;
}
double res = FixedFive(pos - 1);
return res;
}
public static double FixedFive1(double pos)
{
double i = 1.05;
if (pos <= 0)
return 0;
if (pos == 1 || pos == 2)
{
return 10000;
}
if (pos % 3 == 0)
{
return FixedFive1(pos - 1) * i ;
}
double res = FixedFive1(pos - 1) + FixedFive1(pos - 2);
return res;
}
}
}