码迷,mamicode.com
首页 > 其他好文 > 详细

【模拟,时针分针秒针两两夹角】【没有跳坑好兴奋】hdu - 5387 (多校#8 1008)

时间:2015-08-13 19:52:17      阅读:147      评论:0      收藏:0      [点我收藏+]

标签:

算是最好写的一道题了吧,最近模拟没手感,一次过也是很鸡冻o(* ̄▽ ̄*)o

注意事项都在代码里,没有跳坑也不清楚坑点在哪~

技术分享
  1 #include<cstdio>
  2 #include<cstdlib>
  3 #include<cstring>
  4 #include<vector>
  5 #include<cmath>
  6 #include<sstream>
  7 #include<iostream>
  8 using namespace std;
  9 const double eps = 1e-8;
 10 int hh, mm, ss;
 11 int gcd(int a, int b)
 12 {
 13     return b == 0 ? a : gcd(b, a%b);
 14 }
 15 void sub(int a, int b, int c, int d)
 16 { //分数相减(a/b - c/d = (ad-bc)/bd),各种凌乱的特判,注意取绝对值输出
 17     if(c == 0) //c/d = 0,直接输出a/b
 18     {
 19         if(b == 1)  printf("%d", abs(a));
 20         else        printf("%d/%d", abs(a), abs(b));
 21         return;
 22     }
 23     int t1 = b*d, t2 = a*d - b*c;
 24     if(t1 == 0 || t2 == 0) //分子或分子为0,直接输出0
 25     {
 26         printf("0");
 27         return;
 28     }
 29     int t12 = gcd(t1, t2); //化简
 30     int tt1 = t1/t12;
 31     int tt2 = t2/t12;
 32     if(tt1 == 1)    printf("%d", abs(tt2));
 33     else            printf("%d/%d", abs(tt2), abs(tt1));
 34 }
 35 
 36 void get_ang(double a1, double a2, int t11, int t12, int t21, int t22)
 37 {
 38     if(fabs(a1 - a2) < eps) //两角相等
 39     {
 40         printf("0");
 41         return ;
 42     }
 43     if(fabs(a1 - a2) > 180) //注意两角相差大于180度
 44     {
 45         if(a1 > a2)
 46         {
 47             t11 = t11 - 360*t12; //大角减360度,然后二者相减去绝对值,这里动手画一下就清楚了
 48             sub(t11, t12, t21, t22);
 49         }
 50         else
 51         {
 52             t21 = t21 - 360*t22;
 53             sub(t21, t22, t11, t12);
 54         }
 55     }
 56     else if(fabs(a1 - a2) <= 180)
 57     {
 58         if(a1 > a2)
 59             sub(t11, t12, t21, t22);
 60         else
 61             sub(t21, t22, t11, t12);
 62     }
 63 }
 64 void solve()
 65 {
 66     int h = hh*3600 + mm*60 + ss; //化成秒为单位
 67     int m = mm*60 + ss;
 68     int s = ss;
 69     int ansh1, ansh2, ansm1, ansm2, anss1, anss2; //主要化简成分数计算
 70     int gh, gm;        //时针化为秒数与120的最大公约数,分针化为秒数与10的最大公约数
 71     double ah, am, as; //用double比较三针走过的度数大小
 72     if(h)
 73     {
 74         gh = gcd(h, 120);
 75         ansh1 = h/gh, ansh2 = 120/gh;
 76         ah = (double)ansh1/ansh2;
 77     }
 78     else
 79     {
 80         ansh1 = 0, ansh2 = 0;
 81         ah = 0.0;
 82     }
 83     if(m)
 84     {
 85         gm = gcd(m, 10);
 86         ansm1 = m/gm, ansm2 = 10/gm;
 87         am = (double)ansm1/ansm2;
 88     }
 89     else
 90     {
 91         ansm1 = 0, ansm2 = 0;
 92         am = 0.0;
 93     }
 94     if(s)
 95     {
 96         anss1 = s*6, anss2 = 1;
 97         as = (double)anss1/anss2;
 98     }
 99     else
100     {
101         anss1 = 0, anss2 = 0;
102         as = 0.0;
103     }
104     get_ang(ah, am, ansh1, ansh2, ansm1, ansm2); printf(" ");
105     get_ang(ah, as, ansh1, ansh2, anss1, anss2); printf(" ");
106     get_ang(am, as, ansm1, ansm2, anss1, anss2); printf(" ");
107 }
108 int main()
109 {
110     int T;
111     scanf("%d", &T);
112     getchar();
113     while(T--)
114     {
115         string str;
116         cin >> str;
117         for(int i = 0; i < str.length(); i++)
118             if(str[i] == :)
119                 str[i] =  ; //方便用stringstream,用这个耗时会比较大,处理方法随意~
120 
121         stringstream SS(str);
122         SS >> hh;
123         SS >> mm;
124         SS >> ss;
125         if(hh == 12 && (mm || ss)) hh -= 12;
126         else if(hh > 12) hh -= 12; //24小时制转为12小时
127         solve();
128         printf("\n");
129     }
130     return 0;
131 }
hdu-5387

 

【模拟,时针分针秒针两两夹角】【没有跳坑好兴奋】hdu - 5387 (多校#8 1008)

标签:

原文地址:http://www.cnblogs.com/LLGemini/p/4728234.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!