Description
Jonas is developing the JUxtaPhone and is tasked with animating the compass needle. The API is sim- ple: the compass needle is currently in some direc- tion (between 0 and 359 degrees, with north being 0, east being 90), and is being animated by giving the degrees to spin it. If the needle is pointing north, and you give the compass an input of 90, it will spin clockwise (positive numbers mean clockwise direction) to stop at east, whereas an input of −45 would spin it counterclockwise to stop at north west. cc-by NCPC 2016 The compass gives the current direction the phone is pointing and Jonas’ task is to animate the needle taking the shortest path from the current needle direction to the correct direction. Many ifs, moduli, and even an arctan later, he is still not convinced his minimumDistance function is correct; he calls you on the phone.
Input
There will be several test cases. For the each case, The first line of input contains an integer n1 (0 ≤ n1 ≤ 359) ,the current direction of the needle. The second line of input contains an integer n2 (0 ≤ n2 ≤ 359) , the correct direction of the needle.
Output
Output the change in direction that would make the needle spin the shortest distance from n1 to n2. A positive change indicates spinning the needle clockwise, and a negative change indicates spinning the needle counter-clockwise. If the two input numbers are diametrically opposed, the needle should travel clockwise. I.e., in this case, output 180 rather than −180.
Sample Input
315 45 180 270 45 270
Sample Output
90 90 -135
题意
给定初始位置和终止位置,要求转动的角度最小。
代码
#include<stdio.h>
int main()
{
int n1,n2;
while(~scanf("%d%d",&n1,&n2))
{
int temp=n2-n1;
if(temp<0)
temp+=360;
else
temp=temp%360;
if(temp<=360-temp)
printf("%d\n",temp);
else
printf("%d\n",temp-360);
}
return 0;
}
本文介绍了一个简单的算法,用于计算从当前指南针方向到目标方向所需的最小旋转角度。该算法通过比较顺时针和逆时针旋转的距离来确定最短路径。
320

被折叠的 条评论
为什么被折叠?



