如何确定时钟的秒针位于较大的区域还是较小的区域 [英] How to find if the second hand of a clock lies in the larger area or smaller one

查看:92
本文介绍了如何确定时钟的秒针位于较大的区域还是较小的区域的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经以以下格式给出了时间: hh:mm:ss

I have been given the time in the format: hh:mm:ss

我必须找到如果在给定时间内,秒针位于时针和分针所形成的更大或更小区域内?

I have to find if for a given time, the second hand lies in the larger or smaller area formed by the hour and minute hands?

我知道时针的移动速度为每分钟0.5度,分针以每分钟6度的速度移动
,而秒针则以每分钟360度的速度完成。

I know that the hour hand moves at the rate of 0.5 degrees per minute, the minute hand moves at the rate of of 6 degrees per minute and the second hand completes 360 degrees per minute.

无法找出秒针位于哪个区域。那么我该怎么办呢?

But I am unable to find out in which area the second hand lies. So how can I do this?

Second hand within angle between hour and minute hands:
10:15:00
04:40:30

Second hand in reflex angle:
12:01:30


推荐答案

这个问题引起了我的兴趣,所以我继续在 C#中编写了一个测试项目。据我所知,它必须进行测试以确保可以。

The problem intrigued me so I went ahead and wrote a test project in C#. As far as I can tell it works, you will have to test it to make sure though.

这是代码:

string strTime = "10:15:00";

DateTime dt = DateTime.ParseExact(strTime, "HH:mm:ss", System.Globalization.CultureInfo.InvariantCulture);

int nHourDegrees = (360 / 12) * dt.Hour;
int nMinuteDegrees = (360 / 60) * dt.Minute;
int nSecondDegrees = (360 / 60) * dt.Second;

if (nHourDegrees > nMinuteDegrees)
{
    int nArea1 = nHourDegrees - nMinuteDegrees;
    int nArea2 = 360 - nArea1;

    bool bArea1IsBigger = (nArea1 >= nArea2);
    if (nSecondDegrees <= nHourDegrees && nSecondDegrees >= nMinuteDegrees)
    {
        //Second hand lies in area1
        if (bArea1IsBigger)
        {
            Console.WriteLine("Second hand is in the larger area");
        }
        else
        {
            Console.WriteLine("Second hand is in the smaller area");
        }
    }
    else
    {
        if (bArea1IsBigger)
        {
            Console.WriteLine("Second hand is in the smaller area");
        }
        else
        {
            Console.WriteLine("Second hand is in the larger area");
        }
    }
}
else if (nMinuteDegrees > nHourDegrees)
{
    int nArea1 = nMinuteDegrees - nHourDegrees;
    int nArea2 = 360 - nArea1;

    bool bArea1IsBigger = (nArea1 >= nArea2);
    if (nSecondDegrees <= nMinuteDegrees && nSecondDegrees >= nHourDegrees)
    {
        //Second hand lies in area1
        if (bArea1IsBigger)
        {
            Console.WriteLine("Second hand is in the larger area");
        }
        else
        {
            Console.WriteLine("Second hand is in the smaller area");
        }
    }
    else
    {
        if (bArea1IsBigger)
        {
            Console.WriteLine("Second hand is in the smaller area");
        }
        else
        {
            Console.WriteLine("Second hand is in the larger area");
        }
    }
}
else
{
    if (nSecondDegrees == nHourDegrees)
    {
        Console.WriteLine("Second hand is on both of the other hands");
    }
    else
    {
        Console.WriteLine("Second hand is in the ONLY area");
    }
}

我们的想法是找到小时之间的区域分针。然后检查秒针是否在该区域内。我们还将这个区域与另一个区域进行比较,然后我们可以轻松推断出秒针位于两者中的较小者还是较大者中。

The idea is that we find the areas between the Hour and Minute hands. Then check to see if the second hand is inside this area. We also compare this area to the other one and then we can easily deduce if the second hand is in the smaller or larger of the two.

注意:可以对代码进行一些改进:

Note: Some improvements can be made to the code:


  1. 它可以大大缩短-我没有这样做,因为它主要是测试/证明如何做到这一点

  2. 如果小时数超支(即24小时制而不是12小时制),则必须进行更改。即减去12

  3. 如果时间变为12/60/60而不是回到0,则必须手动完成

  4. 常量可以添加以消除对魔术数字的需求

  5. 与上述类似,但常见的计算如 360/12 可以移为常数

  6. 可以分解为单独的方法以提高可读性

  7. 可以移入辅助方法,即 bool IsInLargerArea(字符串timeString)

  8. 需要添加面积相同时的大小写,此刻我只是假设 Area1 如果相等,则更大,即> = (大于或等于)

  9. 将支票添加到确保 straTimes 数组只有3个部分

  10. 可能还有一些我没想到的事情

  1. It can be significantly shorter - I haven't done this because it was mainly a test/proof of how this can be done
  2. If the hours overrun (i.e. 24 hour clock not 12) an alteration will have to be made. i.e. minus 12
  3. If the times go to 12/60/60 and not back to 0, this will have to be done manually
  4. Constants can be added in to remove the need for magic numbers
  5. Similar to the above but common calculations like 360 / 12 can be moved to constants
  6. It can be broken out into separate methods to improve readability
  7. Can be moved into a helper method i.e. bool IsInLargerArea(string timeString)
  8. Need to add the case for when the areas are the same size, at the moment I just assume Area1 to be bigger if they are equal i.e. >= (greater than or equal to)
  9. Add checks to make sure there are only 3 parts to the straTimes array
  10. And possibly some more things that I have not thought of

这篇关于如何确定时钟的秒针位于较大的区域还是较小的区域的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆