hdu5387 Clock(分数加减法模拟及分数化简)
2015-08-14 08:49
253 查看
Link:http://acm.hdu.edu.cn/showproblem.php?pid=5387
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 292 Accepted Submission(s): 199
Problem Description
Give a time.(hh:mm:ss),you should answer the angle between any two of the minute.hour.second hand
Notice that the answer must be not more 180 and not less than 0
Input
There are T(1≤T≤104) test
cases
for each case,one line include the time
0≤hh<24,0≤mm<60,0≤ss<60
Output
for each case,output there real number like A/B.(A and B are coprime).if it's an integer then just print it.describe the angle between hour and minute,hour and second hand,minute and second hand.
Sample Input
Sample Output
Source
2015 Multi-University Training Contest 8
编程思想:算出时针、分针、秒针从00:00:00开始至给出的制定某一时刻这段时间转过的总角度,再求出走最后一圈时在该时刻对应的角度,两两之间相减的结果取绝对值后即为两两之间的角度。注意是钝角时要用360-该钝角,得到小于等于180的角度。因为答案是分数形式,所以运算时要化成下面分子/分母的形式 ,然后按上述过程模拟分数减法操作,最后分子分母化简即可。
AC code:
Clock
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 292 Accepted Submission(s): 199
Problem Description
Give a time.(hh:mm:ss),you should answer the angle between any two of the minute.hour.second hand
Notice that the answer must be not more 180 and not less than 0
Input
There are T(1≤T≤104) test
cases
for each case,one line include the time
0≤hh<24,0≤mm<60,0≤ss<60
Output
for each case,output there real number like A/B.(A and B are coprime).if it's an integer then just print it.describe the angle between hour and minute,hour and second hand,minute and second hand.
Sample Input
4 00:00:00 06:00:00 12:54:55 04:40:00
Sample Output
0 0 0 180 180 0 1391/24 1379/24 1/2 100 140 120 Hint每行输出数据末尾均应带有空格
Source
2015 Multi-University Training Contest 8
编程思想:算出时针、分针、秒针从00:00:00开始至给出的制定某一时刻这段时间转过的总角度,再求出走最后一圈时在该时刻对应的角度,两两之间相减的结果取绝对值后即为两两之间的角度。注意是钝角时要用360-该钝角,得到小于等于180的角度。因为答案是分数形式,所以运算时要化成下面分子/分母的形式 ,然后按上述过程模拟分数减法操作,最后分子分母化简即可。
AC code:
#include <map> #include <cmath> #include <queue> #include <cstdio> #include <vector> #include <iostream> #include <algorithm> #include <set> #define LL long long #define MAXN 1000010 using namespace std; int gcd(int a,int b) { if(b==0) return a; return gcd(b,a%b); } int main() { int T,n,i,hfm,hfz,mfm,mfz,sfm,sfz,g; double h,m,s,hj,mj,sj,aa,bb,cc,tt; double ans1,ans2,ans3; scanf("%d", &T); while(T--) { scanf("%lf:%lf:%lf",&h,&m,&s); sj=(h*3600+m*60+s)*6;//秒针转过的角度,因为答案是分数形式,所以运算时要化成下面分子/分母的形式 //mj=h*60*6+m*6+s/60*6;//分针转过的角度,因为答案是分数形式而除法这样写会有误差,所以运算时要先化成下面分子/分母的形式 //hj=(h+m/60+s/3600)*30;//时针转过的角度,因为答案是分数形式,而除法这样写会有误差,所以运算时要化成下面分子/分母的形式 while(sj>=360) { sj-=360; } sfz=sj;//秒针转过角度的分子部分 sfm=1;//秒针转过角度的分母部分 mfz=h*60*6*60+m*6*60+s*6;//分针转过角度的分子部分 mfm=60;//分针转过角度的分母部分 hfz=30*3600*h+30*60*m+30*s;//时针转过角度的分子部分 hfm=3600;//时针转过角度的分母部分 mj=mfz*1.0/mfm;//分针转过角度 hj=hfz*1.0/hfm;//时针转过角度 while(mj>360) { mj-=360; mfz-=360*mfm;//模拟分数减法 } while(hj>360) { hj-=360; hfz-=360*hfm;//模拟分数减法 } if(hj>mj)//用分数形式的时针角度减去分针角度 { int ans11=hfz*mfm-hfm*mfz;//减完的分子 int ans12=hfm*mfm;//减完的分子母 tt=ans11*1.0/ans12;//减完的角度,即为时针与分针的角度 if(tt>180)//时针与分针的角度为钝角 { ans11=360*ans12-ans11;//360-时针角与分针角度的分数形式 } if(ans11%ans12==0||ans11==0) printf("%d ",ans11/ans12);//分数化简 else { g=gcd(fabs(ans11),ans12); printf("%d/%d ",ans11/g,ans12/g); } } else//同理,下面的代码类似上面的注释部分,这里就不一一给出了 { ans1=mj-hj; int ans11=mfz*hfm-mfm*hfz; int ans12=mfm*hfm; tt=ans11*1.0/ans12; if(tt>180) { ans11=360*ans12-ans11; } if(ans11%ans12==0||ans11==0) printf("%d ",ans11/ans12); else { g=gcd(fabs(ans11),ans12); printf("%d/%d ",ans11/g,ans12/g); } } if(hj>sj) { int ans11=hfz*sfm-hfm*sfz; int ans12=hfm*sfm; tt=ans11*1.0/ans12; if(tt>180) { ans11=360*ans12-ans11; } if(ans11%ans12==0||ans11==0) printf("%d ",ans11/ans12); else { g=gcd(fabs(ans11),ans12); printf("%d/%d ",ans11/g,ans12/g); } } else { int ans11=sfz*hfm-sfm*hfz; int ans12=sfm*hfm; tt=ans11*1.0/ans12; if(tt>180) { ans11=360*ans12-ans11; } if(ans11%ans12==0||ans11==0) printf("%d ",ans11/ans12); else { g=gcd(fabs(ans11),ans12); printf("%d/%d ",ans11/g,ans12/g); } } if(sj>mj) { int ans11=sfz*mfm-sfm*mfz; int ans12=sfm*mfm; tt=ans11*1.0/ans12; if(tt>180) { ans11=360*ans12-ans11; } if(ans11%ans12==0||ans11==0) printf("%d \n",ans11/ans12); else { g=gcd(fabs(ans11),ans12); printf("%d/%d \n",ans11/g,ans12/g); } } else { ans1=mj-sj; int ans11=mfz*sfm-mfm*sfz; int ans12=mfm*sfm; tt=ans11*1.0/ans12; if(tt>180) { ans11=360*ans12-ans11; } if(ans11%ans12==0||ans11==0) printf("%d \n",ans11/ans12); else { g=gcd(fabs(ans11),ans12); printf("%d/%d \n",ans11/g,ans12/g); } } } return 0; }
相关文章推荐
- Java 实现装饰(Decorator)模式
- Activiti 部分实用功能
- poj 2253 Frogger
- Spring 学习笔记(三)——AOP
- 致爱丽丝 之MY收藏
- nslog格式转换
- web乱码的根源
- Codeforces 570A Elections
- 前端模块化开发的价值
- 免费微信文章配图库
- 黑马程序员 Java基础 反射
- 正则表达式
- A题之字符串末尾补0
- poj 2031 Building a Space Station 【最小生成树 Prim】
- 刘备是怎样“融资”创业的?
- 子网划分示例和寻址过程
- Python lxml xpath
- mysql在linux平台二进制包安装方式
- json解析出来的数据库是字典和数组的组合,创建一个model来接收解析数据
- C_获得当前的工作路径 _getcwd()函数:取得当前的工作目录