3525 - A.Clock
Clock is invented by ancient Arabic engineers and which contributes to build the concept of accurate time for us human beings and even could be essential tool that widely used in industry, business and our routine lives. Nevertheless, the ideology of clock turns out to be quite simply that even make sense to little kids. We could hardly imagine that how do Arabic wisdom come up with such idea to indicate time only by two or three fingers. The other day, Tim are asking Jim and Hysramp to play his newly invented game describe as follow. Tim randomly change the time of his lovely alarm clock then ask Jim and Hysramp to tell the degree between hour finger and minute finger. Jim seems quite gifted playing it while Hysramp does not. When Hysramp fails to answer Tim, Tim smiles to Hysramp, and Hysramp smiles to you, an ace programmer.
题目输入
The first line of the input contains one integer T, which indicate the number of test cases. Each test case contains one indicating the time on Tim 's clock in the form of HH:MM. (0 ≤ HH < 24, 0 ≤ MM < 60)
题目输出
One line for each test case contains only one number indicating the answer. An integer or an irreducible fraction indicated the degree D between hour finger and minute finger (0 ≤ D<360).
输入/输出样例
输入格式
19 00:00 09:45
输出格式
0 45/2 181/2
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); } } } }