算法 按比例递增求和

 

需求:

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;
        }
    }
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值