首页 > 试题广场 >

魔法表

[编程题]魔法表
  • 热度指数:1887 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 32M,其他语言64M
  • 算法知识视频讲解
时辰送给了她的女儿凛一块魔法表,但是魔法表的表针总是指向奇怪的地方,所以凛决定修理一下这块表。当前表的指针指向了一个方向 𝑛1 (在 0 度到 359 度之间,正北方向是 0 度, 正东方向是 90 度),她需要将表针调节到方向 𝑛2 。她可以选择顺时针旋转表针,也可以逆时针旋转表针,若顺时针旋转的话,角度会增大,逆时针旋转则角度会减小。当顺时针旋转 到 359 度后,若再旋转一度,则会回到 0 度。凛想要让表针旋转的角度尽量小,也就是以 最短路径旋转到正确的方向,请你告诉她应该如何旋转。 当有多种旋转方式能够旋到正确的方向,且旋转过的角度相同时,凛会选择顺时针旋转。

数据范围:

输入描述:
第一行包含一个整数𝑛1 ,表示表针当前的方向。

第二行包含一个整数𝑛2 ,表示表针应该指向的正确方向。


输出描述:
若需要顺时针旋转𝑥度,则输出𝑥;

若需要逆时针旋转𝑥度,输出−𝑥。
示例1

输入

315
45

输出

90
示例2

输入

45
270

输出

-135
感觉就像打了一场圣杯战争,怎么想都是时辰的错ww
import java.util.*;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n1 = sc.nextInt(), n2 = sc.nextInt();
        int clockwise = 0, anticlockwise = 0;
        if (n1 > n2) {
            clockwise = n2 - (n1 - 360);
            anticlockwise = n1 - n2;
        }
        else {
            clockwise = n2 - n1;
            anticlockwise = 360 - (n2 - n1);
        }
        int ans = clockwise <= anticlockwise ? clockwise : -anticlockwise;
        System.out.println(ans);
    }
}

发表于 2019-03-05 21:27:31 回复(0)
没什么难度,直接模拟这个旋转指针的过程就好了
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n1 = sc.nextInt();
        int n2 = sc.nextInt();
        // 顺时针旋转的角度
        int clockwise = n1 <= n2? n2 - n1:360 - n1 + n2;
        // 逆时针旋转的角度
        int anti_clockwise = n1 <= n2? -n1 - (360 - n2): n2 - n1;
        // 顺时针旋转的角度如果小于等于逆时针就顺时针转,否则逆时针转
        System.out.println(Math.abs(clockwise) <= Math.abs(anti_clockwise)? clockwise: anti_clockwise);
    }
}


发表于 2021-02-03 17:32:50 回复(0)
把第一个时间置零,第二个时间加上第一个时间需要置零旋转的角度。两个指针之间的角度不变,之后就很简单了。
#include <bits/stdc++.h>
using namespace std;

int main()
{
	int z, x;
	cin >> z >> x;
	if (z > x)
	{
		x += 360 - z;
		z = 0;
	}
	if (x - z <= 180)cout << x - z;
	else cout << "-" << 360 - x + z;
}


发表于 2020-04-29 22:28:06 回复(0)
import java.util.Scanner;

/**
 * @author wylu
 */
public class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int n1 = scanner.nextInt(), n2 = scanner.nextInt();
        int v1 = Math.abs(n1 - n2), v2 = 360 - v1, v = Math.min(v1, v2);
        System.out.println((n1 + v) % 360 == n2 ? v : -v);
    }
}

发表于 2019-03-13 21:05:00 回复(0)
#include <bits/stdc++.h>

using namespace std;

int main()
{     int n1,n2;     while(cin>>n1>>n2){         int d = n2-n1;         if(d>=0 && d<=180)             cout<<d<<endl;         else if(d>180 && d<360)             cout<<d-360<<endl;         else if(d>=-180 && d<0)             cout<<d<<endl;         else if(d>-360 && d<-180)             cout<<360+d<<endl;     }     return 0;
}

编辑于 2019-01-31 02:28:21 回复(0)
#include<stdio.h>
int main()
{
    int start = 0;
    int end = 0;
    int rotate = 0;
    int delta = 0;
    scanf("%d", &start);
    scanf("%d", &end);
    if(start>=end)
    {
        delta = start - end;
        rotate = delta > (360-delta)?(360-delta):-delta;
    }
    else
    {
        delta = end - start;
        rotate = delta >(360-delta)?-(360-delta):delta;
    }
    printf("%d", rotate);
    return 0;
}

发表于 2021-10-05 08:10:41 回复(0)
输入的读取完全扯淡,明明是一行以空格分开的两个数,害我调试了好长时间。
def solve(n1,n2):
    if n2>=n1:
        ans1 = n2-n1
        ans2 = -(360-n2+n1)
    else:
        ans1 = n2-n1
        ans2 = 360-n1+n2
    if abs(ans1)>abs(ans2):
        return ans2
    else:
        return ans1
if __name__=='__main__':
    current,good = list(map(int,input().split()))
    print(solve(current,good))


发表于 2019-09-15 15:37:41 回复(0)
#include <iostream>

using namespace std;

int main(){
    int n1,n2;
    cin >> n1 >>n2;
    if((n2 + 360 - n1)%360 <= 180)
        cout << (n2 +360-n1)%360;
    else{
        cout << -1 *(360 - (n2 +360-n1)%360);
    }
    return 0;
}
其实很简单!
发表于 2019-09-02 23:17:56 回复(0)
import java.util.Scanner;
public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int start = sc.nextInt();
        int end = sc.nextInt();
        int res = end - start;
        int mul  = 1;
        if (Math.abs(res) > 180) {
            if (res > 0) {
                mul = -1;
            }
            res = mul * (360 - Math.abs(res));
        }
        System.out.println(res);
    }
}
发表于 2019-06-16 15:33:54 回复(0)
#include<bits/stdc++.h>

using namespace std;

int main()
{
    int n1,n2;
    cin >> n1;
    cin >> n2;
    int ans1 = 0, ans2 = 0; // 顺时针,逆时针
    if(n1 < n2)
    {
        ans1 = n2 - n1;
        ans2 = n1 + 360 - n2;
    }
    else{
        ans1 = 360 - n1 + n2;
        ans2 = n1 - n2;
    }
    if(ans1 == ans2)
            cout << ans1 << endl;
        else if(ans1 > ans2)
            cout << -ans2 << endl;
        else
            cout << ans1 << endl;
    return 0;
}

编辑于 2019-05-27 16:12:30 回复(0)
import java.util.Scanner;
public class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int n1 = scanner.nextInt();
        int n2 = scanner.nextInt();
        System.out.println(solve(n1, n2));
    }
    private static int solve(int n1, int n2) {
        int res1 = (n2 - n1 + 360) % 360;
        int res2 = (n1 - n2 + 360) % 360;
        return res1 <= res2 ? res1 : -res2;
    }
}

编辑于 2019-05-26 11:21:47 回复(0)
import java.util.*;
public class  Main{
    public static void main(String[] args){
        Scanner input = new Scanner(System.in);
        int n1 = input.nextInt();
        int n2 = input.nextInt();
        int result = 0;
        
        if(n1<=n2){
            int shun = n2-n1;
            int ni = 360-n2+n1;
            if(shun<=ni)
                System.out.println(shun);
            else{
                System.out.println(ni-2*ni);
            }
        }
        else{
            int shun = 360-n1+n2;
            int ni = n1-n2;
            if(shun<=ni)
                System.out.println(shun);
            else{
                System.out.println(ni-2*ni);
            }
        }
    }
}


发表于 2019-04-22 17:30:37 回复(0)