c#数学/公式计算

时间:2017-11-17 15:30:49

标签: c# math formula

我有一个有趣的问题,我想在我的c#项目中解决,我的脑袋受伤了所以任何建议都会受到高度赞赏

基本上我有一名员工在3周的周期内工作

第1周 - 他们工作了

第2周 - 他们结婚了

wk 3 - 他们工作了

第4周 - 他们工作了

第5周 - 他们结婚了 ..等等

我需要一个公式,告诉我他们在任何一周工作的那一天,即他们在第49周就业的哪一天工作?

关于我如何解决这个等式的任何想法?

干杯 安东尼

2 个答案:

答案 0 :(得分:2)

正如评论中所讨论的,我们可以在公式中使用modulo(%)运算符。

int dayInt = (nthWeek % 3);

  

这将给你结果0,1,2。0 - 星期五,1 - 星期一,2-   星期三

以下是代码:

int nthWeek = 49;

    int result = (nthWeek % 3);

    switch(result)
    {
        case 0:
            Console.WriteLine("Worker is working on Friday");
        break;

        case 1:
            Console.WriteLine("Worker is working on Monday");
        break;

        case 2:
            Console.WriteLine("Worker is working on Wednesday");
        break;

        default:
            Console.WriteLine("Worker is working on other day");
        break;
    }

实施:DotnetFiddler

答案 1 :(得分:0)

这将提供所需的输出:

using System;
using System.Collections.Generic;

public static void WhichDaysAreTheyWorking()
{
   List<String> daysOfTheWeek = new List<String>();
   daysOfTheWeek.Add("Monday");
   daysOfTheWeek.Add("Tuesday");
   daysOfTheWeek.Add("Wednesday");
   daysOfTheWeek.Add("Thursday");
   daysOfTheWeek.Add("Friday");
   daysOfTheWeek.Add("Saturday");
   daysOfTheWeek.Add("Sunday");
   int i = 0;
   for (int week = 1; week < 50; week++)
   {
      if (daysOfTheWeek[i].Equals("Friday"))
      {
          Console.WriteLine("Week # " + week + " They work " + daysOfTheWeek[i]);
          Console.WriteLine("Week # " + (week + 1) + " They work " + daysOfTheWeek[i - 4]);
          i = 2;
          week = week + 1;
          continue;
      }
      else
      {
          Console.WriteLine("Week # " + week + "They work " + daysOfTheWeek[i]);
          i = i + 2;
      }
   }
   Console.ReadKey();
}