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

时间:2023-06-22 23:36:02

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

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

 #include<cstdio>
#include<cstdlib>
#include<cstring>
#include<vector>
#include<cmath>
#include<sstream>
#include<iostream>
using namespace std;
const double eps = 1e-;
int hh, mm, ss;
int gcd(int a, int b)
{
return b == ? a : gcd(b, a%b);
}
void sub(int a, int b, int c, int d)
{ //分数相减(a/b - c/d = (ad-bc)/bd),各种凌乱的特判,注意取绝对值输出
if(c == ) //c/d = 0,直接输出a/b
{
if(b == ) printf("%d", abs(a));
else printf("%d/%d", abs(a), abs(b));
return;
}
int t1 = b*d, t2 = a*d - b*c;
if(t1 == || t2 == ) //分子或分子为0,直接输出0
{
printf("");
return;
}
int t12 = gcd(t1, t2); //化简
int tt1 = t1/t12;
int tt2 = t2/t12;
if(tt1 == ) printf("%d", abs(tt2));
else printf("%d/%d", abs(tt2), abs(tt1));
} void get_ang(double a1, double a2, int t11, int t12, int t21, int t22)
{
if(fabs(a1 - a2) < eps) //两角相等
{
printf("");
return ;
}
if(fabs(a1 - a2) > ) //注意两角相差大于180度
{
if(a1 > a2)
{
t11 = t11 - *t12; //大角减360度,然后二者相减去绝对值,这里动手画一下就清楚了
sub(t11, t12, t21, t22);
}
else
{
t21 = t21 - *t22;
sub(t21, t22, t11, t12);
}
}
else if(fabs(a1 - a2) <= )
{
if(a1 > a2)
sub(t11, t12, t21, t22);
else
sub(t21, t22, t11, t12);
}
}
void solve()
{
int h = hh* + mm* + ss; //化成秒为单位
int m = mm* + ss;
int s = ss;
int ansh1, ansh2, ansm1, ansm2, anss1, anss2; //主要化简成分数计算
int gh, gm; //时针化为秒数与120的最大公约数,分针化为秒数与10的最大公约数
double ah, am, as; //用double比较三针走过的度数大小
if(h)
{
gh = gcd(h, );
ansh1 = h/gh, ansh2 = /gh;
ah = (double)ansh1/ansh2;
}
else
{
ansh1 = , ansh2 = ;
ah = 0.0;
}
if(m)
{
gm = gcd(m, );
ansm1 = m/gm, ansm2 = /gm;
am = (double)ansm1/ansm2;
}
else
{
ansm1 = , ansm2 = ;
am = 0.0;
}
if(s)
{
anss1 = s*, anss2 = ;
as = (double)anss1/anss2;
}
else
{
anss1 = , anss2 = ;
as = 0.0;
}
get_ang(ah, am, ansh1, ansh2, ansm1, ansm2); printf(" ");
get_ang(ah, as, ansh1, ansh2, anss1, anss2); printf(" ");
get_ang(am, as, ansm1, ansm2, anss1, anss2); printf(" ");
}
int main()
{
int T;
scanf("%d", &T);
getchar();
while(T--)
{
string str;
cin >> str;
for(int i = ; i < str.length(); i++)
if(str[i] == ':')
str[i] = ' '; //方便用stringstream,用这个耗时会比较大,处理方法随意~ stringstream SS(str);
SS >> hh;
SS >> mm;
SS >> ss;
if(hh == && (mm || ss)) hh -= ;
else if(hh > ) hh -= ; //24小时制转为12小时
solve();
printf("\n");
}
return ;
}

hdu-5387