第一场-J-Jumbled Compass

题目链接:点击打开链接

(一)题目描述:

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 integern1(0n1359)

, the current direction of the needle. The second line of input contains an integer n2(0n2359)

, 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<iostream>
#include<cstring>
#include<string>
#include<cstdio>
#include<vector>
#include<queue>
#include<algorithm>
using namespace std;
int main(){
//    freopen("in.txt","r",stdin);
    int x,y;
    while(cin>>x>>y){
        if(x>y){
            int m=x-y;
            if(m<180)cout<<-m<<endl;
            else cout<<360-m<<endl;
        }
        else{
            int m=y-x;
            if(m>180)cout<<-(360-m)<<endl;
            else cout<<m<<endl;
        }
    }
    return 0;
}

	
	



猜你喜欢

转载自blog.csdn.net/xbb224007/article/details/79869353