CSU: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 integer n1(0n1359)n1(0≤n1≤359) , the current direction of the needle. The second line of input contains an integer n2(0n2359)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


思路:这题很简单,基本上就是这场比赛的破零题。题意告诉我们在圆周里面(0度到359度)中给两个值,以顺时针旋转为正值,求怎么旋转使快一点从先给的值变成后面的值,我们根据数学知识很快知道,把大的角度减去小的角度就是顺时针旋转的差距,把大的角度减去360,再用小的角度减去该值就是逆时针旋转的差距(或者把小的角度加上360减去大的角度)。

还需要注意的就是给值的时候是大的角度在前面还是小的角度在前面。就两个需要注意的点。直接附上ac代码吧

#include<iostream>
#include<algorithm>
#include<iomanip>
#include<cstring>
#include<cmath>
using namespace std;
int n,m;
int main()
{
	while(cin>>n>>m)
	{
		if(n<m)
		{ swap(n,m);
		int temp=n-360;
		if(abs(n-m)>abs(m-temp))
		  cout<<-(m-temp)<<endl;
		else
		  cout<<n-m<<endl;
     	}
		else
		{
			int temp=n-360;
		   if(abs(n-m)>abs(m-temp))
		     cout<<m-temp<<endl;
		    else
		      cout<<-(n-m)<<endl;
		}
		  
	}
	return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值