C#中的工作时间表

时间:2014-02-12 04:23:55

标签: c# console

我正在尝试用c#编写一个小程序,在那里我可以显示分开的夜间守卫的工作时间表。

这个想法是,警卫必须每周三开始工作一周的第一周

警卫也应该从第6周开始每隔5周进行一次“夜班”工作

我在代码中实现这一点时遇到了一些麻烦,所以我想我会请一些灵魂寻求帮助。

这是我到目前为止所做的:)

int[] _numbers = new int[52];
List<int> _weekends = new List<int>();
List<int> _nights = new List<int>();

for(int i = 0; i < _numbers.Length; i++)
{
     i += 2;
     _weekends.Add(i);
}

int num = 0;

foreach (int j in _weekends)
{
     num = j + 2;

     Console.WriteLine(num);
}

1 个答案:

答案 0 :(得分:0)

好的,我会给你一些提示。

  1. 如果这个人应该从第1周开始每隔三个周末工作一次......你为什么要加2?
    • 第一个周末 - 作品
    • 第二个周末 - 关闭
    • 第3个周末 - 关闭
    • 第四个周末 - 工作(这是最后一个周末之后的第三个周末。注意这是他上一次工作后的3个周末,而不是2 ...
  2. 接下来,

    1. 夜班......你的代码中还没有,但它是相同的逻辑:

      • 第1周 - 关闭
      • 第2周 - 关闭
      • 第3周 - 关闭
      • 第4周 - 关闭
      • 第5周 - 关闭
      • 第6周 - 工作......下次他工作时你应该加几周?
    2. 现在,你的数组52表示周和周末,每个数字代表我假设一年中的一周。

      你真的不需要做任何添加。你实际上可以循环浏览你的52个号码,并且有两个条件,一个用于周末,一个用于夜晚。

      这是一个数学问题,真的......

      如何在周末获得1,4,7等,以及在晚上获得6,11,16等?这里有一个可重复的模式,如果你从每个集合中删除1,你得到3,6,9,...,以及5,10,15等。这些是易于整除的简单集合。当你循环时,你可以检查每个数字,看看它是否可被整除,而不是每个集合中的乘法因子的余数(即,乘法因子是什么让你得到3,6,9等等。(提示:与数字相同)轮班之间的天数。当没有余数时,你就会获得累积奖金。

      假设你有一个循环(顺便说一句,你真的不需要列表中的数字,这是一个要求吗?:

      var numberOfWeeksInYear = 52 // define it so you don't use a "magic number"
      for (var i = 0; i < numberOfWeeksInYear; i++) // easier here to use for vs. foreach because you can use the value of i to get your calculations done
      {
          // Condition for weekends
          // if (i is divisible by 3 with no remainder) you get 3, 6, 9, etc.
          // that formula plus 1 gets you 4, 7, etc which is the weekend shift 
          // hint: Look into the mod operator or Math.DivRem()
          // fill in the blanks.
      
          // Condition for nights
          // Ah! same thing, you need 6, 11, 16, etc.
          // Well if you divide i by 5 and add 1 then you get that number. Your solution is the same
          // as above...
      }
      

      现在,如果您必须使用foreach而不是for,那么只需保留您的列表编号并使用计算中的实际值:

      foreach(var number in _numbers)
      {
          // if number is divisible by .... etc.
      }
      

      这应该指向正确的方向,而不是直接给你答案。 祝你好运!