应该是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();
        }
    }
}

解决方案 »

  1.   

    难道你说的是时段价格,不是时段每小时的价格。如果是这样,代码如下。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 int 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).TotalHours)
                    .Select(x => rate.Single(y => y.Value.Contains(start.AddHours(x).Hour)).Key).Distinct().Sum();
            }
        }
    }30
    Press any key to continue . . .
      

  2.   

    我们公司很老的项目了 用的是2005开发的。能不能在写个2005的 没有linq的啊 十分感谢版主
      

  3.   

    那就自己算呗double sum = 0;foreach (var item in rate)
    {
        for (int i = 0; i < item.Value.Count; i++)
        {
            if (item.Value[i] >= start.Hour && item.Value[i] < end.Hour)
            {
                sum += item.Key;
            }
        }
    }return sum;
      

  4.   

    浮点数有点误差。不要紧,你用Convert.ToInt32()转换下就可以了。
      

  5.   

    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 } }
                };
                double sum = 0.0;
                for (int i = 0; i < (int)new TimeSpan(end.Ticks - start.Ticks).TotalMinutes; i++)
                    foreach (KeyValuePair<int, List<int>> item in rate)
                        if (item.Value.Contains(start.AddMinutes(i).Hour)) sum += (item.Key / 60.0);
                return sum;
            }
        }
    }