计蒜客————Jumbled Compass

  •  65.23%
  •  1000ms
  •  65536K


Jonas is developing the JUxtaPhone and is tasked with animating the compass needle. The API is simple: the compass needle is currently in some direction (between 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.

计蒜客————Jumbled Compass_第1张图片

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

The first line of input contains an integer n(≤ n≤ 359), the current direction of the needle.The second line of input contains an integer n(≤ n≤ 359), the correct direction of the needle.

Output

Output the change in direction that would make the needle spin the shortest distance from n1to 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

样例输入1

315
45

样例输出1

90

样例输入2

180
270

样例输出2

90

样例输入3

45
270

样例输出3

-135
给你一个罗盘,给你现在指向的方向,一个正确方向,问如何挪动最少的角度,纠正罗盘.(罗盘指针 0~359,+值为顺时针,-值为逆时针.当+-180都可以时输出180).
 
    
#include
#include
#include
#include
using namespace std;
int main(){
    int n,m;
    while(cin>>n>>m){
        int t=m-n;
        if(t<0) t+=360;
        else t=t%360;
        if(t<=360-t) printf("%d\n",t);
        else printf("%d\n",t-360);
    }
    return 0;
}

你可能感兴趣的:(计蒜客)