我有时间采用以下格式: hh:mm:ss
我必须找到,如果在给定的时间内,秒针位于由时针和分针形成的更大或更小的区域?
我知道时针以每分钟0.5度的速度移动,分针以每分钟6度的速度移动而秒针以每分钟360度的速度移动.
但是我无法找出秒针在哪个区域.那我该怎么做呢?
Second hand within angle between hour and minute hands:
10:15:00
04:40:30
Second hand in reflex angle:
12:01:30
Run Code Online (Sandbox Code Playgroud)
The*_*der 10
这个问题引起了我的兴趣所以我继续写了一个测试项目C#.据我所知,它必须进行测试才能确定.
这是代码:
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");
}
}
Run Code Online (Sandbox Code Playgroud)
我们的想法是找到时针和分针之间的区域.然后检查秒针是否在此区域内.我们还将这个区域与另一个区域进行比较,然后我们可以很容易地推断出秒针是否在两者中较小或较大.
注意:可以对代码进行一些改进:
360 / 12可以移动到常量bool IsInLargerArea(string timeString)Area1它们相等即大>=(即大于或等于)straTimes阵列只有3个部分| 归档时间: |
|
| 查看次数: |
605 次 |
| 最近记录: |