c # to solve a particular pen questions cousin

Scene Description:

Wuhan heatwave Internet fees are as follows:

6: 00-12: 00 charges 5.5 / h, real-time charging

12: 00-18: 00 Pay 6 / h, real-time charging

18: 00-23: 00 Pay 7 / h, real-time charging

23: 00- 6:00 the next day, charge 5 / h, the upper limit charge 15

The upper limit charge refers to real-time charging upper limit is exceeded, according to the price cap, or real-time charging.

A statement period objects:

 1     /// <summary>
 2     /// 时段收费区间
 3     /// </summary>
 4     public class State
 5     {
 6         public TimeSpan start { get; set; }
 7         public TimeSpan end { get; set; }
 8         public double pay { get; set; }
 9         public double maxpay { get; set; }
10 
11         /// <Summary> 
12 is          /// cost within a certain time / hr
 13 is          ///  </ Summary> 
14          ///  <param name = "_ Start"> Start Time </ param> 
15          ///  <param name = "_ End "> end time </ param> 
16          ///  <param name =" _ Pay "> per hour to </ param> 
. 17          ///  <param name =" _ maxpay "> period ceiling charge, representative of real-time charging is 0 < / param> 
18 is          public State (_start the TimeSpan, the TimeSpan the _end, Double _pay, Double _maxpay)
 . 19          {
 20 is              the this .start = _start;
21             this.end= _end;
22             this.pay = _pay;
23             this.maxpay = _maxpay;
24         }
25     }

II. Calculation based

  1     public class Compute
  2     {
  3         List<State> states = null;
  4         public Compute(List<State> _states)
  5         {
  6             this.states = _states;
  7         }
  8 
  9         public double GetResult(DateTime start,DateTime end)
 10         {
 11             double result = 0;
 12             if (this.states != null)
 13             {
 14                 TimeSpan totlaSpan = end - start;
 15                 if (totlaSpan.TotalDays >= 1)
 16                 {
 17                     int days = Convert.ToInt32(totlaSpan.TotalDays);
 18                     result += FullDayPay() * days;
 19                     //证明有余的天数
 20                     if ((totlaSpan.TotalDays - days) > 0)
 21                     {
 22                         this.TimespanPay(start.AddDays(days), end, ref result);
 23                     }
 24                 }
 25                 else
 26                 {
 27                     this.TimespanPay(start, end, ref result);
 28                 }
 29             }
 30             return result;
 31         }
 32 
 33         /// <summary>
 34         /// 按时段收费
 35         /// </summary>
 36         /// <param name="start"></param>
 37         /// <param name="end"></param>
 38         /// <returns></returns>
 39         private voidTimespanPay (the DateTime Start, the DateTime End, REF  Double Result)
 40          {
 41 is              // time period in which the charge section 
42 is              State = spanState the this .GetSpan (Start);
 43 is              // end of this period of time and costs section 
44 is              the DateTime = spanEnd new new the DateTime (start.Year, start.Month, start.Day) .Add (spanState.end);
 45              the TimeSpan span;
 46 is              BOOL Next = to false ;
 47              // If the end time of the segment in the charge section 
48              IF (end < spanEnd)
 49              {
 50                 // calculate the total charge duration junction 
51 is                  span = End - Start;
 52 is              }
 53 is              the else 
54 is              {
 55                  span = spanEnd - Start;
 56 is                  Next = to true ;
 57 is              }
 58              // calculating charges within the interval 
59              Double Payment * = span.TotalHours spanState.pay;
 60              IF (spanState.maxpay> 0 )
 61 is              {
 62 is                  Result + = math.min (Payment, spanState.maxpay);
63 is              }
 64              the else 
65              {
 66                  Result + = Payment;
 67              }
 68              IF (Next)
 69              {
 70                  the this .TimespanPay (spanEnd, End, REF Result);
 71 is              }
 72          }
 73 is  
74          ///  <Summary> 
75          /// the acquisition starting time period belongs to
 76          ///  </ Summary> 
77          ///  <param name = "start"> </ param> 
78          ///  <Returns> </ Returns>
 79         private State GetSpan(DateTime start)
 80         {
 81             IEnumerable<State> states = this.states.Where(a => start >= new DateTime(start.Year, start.Month, start.Day).Add(a.start));
 82             return states.Last();
 83         }
 84 
 85         /// <summary>
 86         /// 获取全天的收费
 87         /// </summary>
 88         /// <returns></returns>
 89         private double FullDayPay()
 90         {
 91             double result = 0;
 92             foreach (State item in this.states)
 93             {
 94                 if (item.maxpay > 0)
 95                 {
 96                     result += item.maxpay;
 97                 }
 98                 else
 99                 {
100                     result += (item.end - item.start).TotalHours * item.pay;
101                 }
102             }
103             return result;
104         }
105     }

III. Calls

 1            List<State> states = new List<State>
 2             {
 3                 new State(new TimeSpan(6,0,0),new TimeSpan(12,0,0),5.5,0),
 4                 new State(new TimeSpan(12,0,0),new TimeSpan(18,0,0),6,0),
 5                 new State(new TimeSpan(18,0,0),new TimeSpan(23,0,0),7,0),
 6                 new State(new TimeSpan(23,0,0),new TimeSpan(1,6,0,0),5,15),
 7             };
 8 
 9             //If the time from the Internet. 17 2019-9-20: 23-2019-9-22 22:10 
10              the DateTime = Start new new the DateTime ( 2019 , . 9 , 20 is , . 17 , 23 is , 0 );
 . 11              the DateTime End = new new the DateTime ( 2019 , . 9 , 22 is , 22 is , 10 , 0 );
 12 is  
13 is              the Compute C = new new the Compute (States);
 14              Console.WriteLine (c.GetResult (Start, End));

 

Guess you like

Origin www.cnblogs.com/shi2310/p/11576501.html