1 条题解

  • 0
    @ 2025-4-14 18:43:49

    C :

    #include<stdio.h>
    int main()
    {
    	int t, hh, mm, sh, sm, res;
    	scanf("%d",&t);
    	while(t--)
    	{
    		scanf("%d:%d",&hh,&mm);
    		if (hh>=12)
    			hh=hh-12;
    		sh=hh*60+mm;
    		sm=mm*12;
    		if (sh>sm)
    			res=sh-sm;	
    		else
    			res=sm-sh;	
    		if (res>=360) res=720-res;	
    		if (res%2==0) 
    			printf("%d\n",res/2);	
    		else 
    			printf("%d/2\n",res);
    	}
    	return 0;
    }
    

    C++ :

    #include <iostream>
    #include <cstdio>
    #include <cmath>
    
    using namespace std;
    const int NN = 101;
    
    double calc(int hour, int min)
    {
    	hour = hour % 12;
    	double dMperM = 360.0 / 60.0;
    	double dHperM = 360.0 / 12.0 / 60;
    	double dHperH = 360.0 / 12.0;
    
    	double dMin = min * dMperM;
    
    	double dHour = hour * dHperH + min * dHperM;
    
    	double d1 = fabs(dMin - dHour);
    	double d2 = 360 - d1;
    
    	if (d1 <= d2)
    		return d1;
    	else
    		return d2;
    
    }
    
    int main()
    {
    	int n;
    	int nHour, nMin;
    	while (cin >> n)
    	{
    		for (int i = 0; i < n; i++)
    		{
    			scanf("%d:%d", &nHour, &nMin);
    
    			double dret = calc(nHour, nMin);			
    						
    			if (dret - (int)dret != 0)
    			{
    				int ret = (int)(dret * 2);
    				cout << ret << "/2" << endl;
    			}				
    			else
    			{
    				int ret = (int)dret;
    				cout << ret << endl;
    			}
    				
    		}
    			
    	}
    
    	return 0;
    }
    
    

    Java :

    import java.util.Scanner;
    
    public class Main {
    
    	public static void main(String[] args) {
    		Scanner in = new Scanner(System.in);
    		String str;
    		int cases = in.nextInt(), h, m, d;
    		str = in.nextLine();
    		for (int i = 0; i < cases; i++) {
    			str = in.nextLine();
    			h = (str.charAt(0) - '0') * 10 + (str.charAt(1) - '0');
    			m = (str.charAt(3) - '0') * 10 + (str.charAt(4) - '0');
    			if (h >= 12)
    				h -= 12;
    			d = Math.abs(m * 11 - 60 * h);
    			if (d > 360) {
    				d = 720 - d;
    			}
    			if (0 == d % 2) {
    				System.out.println(d / 2);
    			} else {
    				System.out.println(d + "/" + 2);
    			}
    		}
    	}
    }
    
    • 1