首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > .NET > C# >

求按时间段划价的算法

2013-12-06 
求按时间段计费的算法有这样几个时间段从至收费00:00~08:000元08:00~10:005元10:00~12:0010元12:00~15:001

求按时间段计费的算法
有这样几个时间段

从       至     收费
00:00~08:00    0元

08:00~10:00    5元

10:00~12:00    10元

12:00~15:00    15元

15:00~00:00    20元

顾客 9点开台   13点结账共消费3元 

求算法!
[解决办法]
应该是40元。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            DateTime dt1 = new DateTime(2013, 12, 5, 9, 0, 0);
            DateTime dt2 = new DateTime(2013, 12, 5, 13, 0, 0);
            Console.WriteLine(foo(dt1, dt2));
        }

        static double foo(DateTime start, DateTime end)
        {
            Dictionary<int, List<int>> rate = new Dictionary<int, List<int>>()
            {
                { 0, new List<int>() { 0, 1, 2, 3, 4, 5, 6, 7 } },
                { 5, new List<int>() { 8, 9 } },
                { 10, new List<int>() { 10, 11 } },
                { 15, new List<int>() { 12, 13, 14 } },
                { 20, new List<int>() { 15, 16, 17, 18, 19, 20, 21, 22, 23 } }
            };
            return Enumerable.Range(0, (int)new TimeSpan(end.Ticks - start.Ticks).TotalMinutes)
                .Select(x => (double)rate.Single(y => y.Value.Contains(start.AddMinutes(x).Hour)).Key / 60.0).Sum();
        }
    }
}

热点排行