博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
zoj 2106 Tick and Tick(比较好的数学题目,代码特麻烦,注意精度)
阅读量:4034 次
发布时间:2019-05-24

本文共 4966 字,大约阅读时间需要 16 分钟。

1、

2、题目大意:

一个钟的三个指针在不停的转动,他们已经厌烦了这样,当他们互相的距离角度大于等于D时,他们会很开心,问一天之中他们happy的时间占总时间的概率。
我觉得这是在解不等式,我原来使用的暴力破解,毫无疑问失败了;我们只要找到某一分钟内,他们happy的时间,然后钟每过12个小时相当于43200秒复原一次。因此总时间就是43200白秒,只要求出在这43200的happy时间,答案就知道了;
假设现在的时钟是h小时,m分钟,s秒,给定的角度为degree;则列出happy的不等式有
时针到0时刻的角度的实际值为hw = (h+m/60+s/3600)*30,分针的角度mw = (m+s/60)*6,秒针的角度为sw = s*6;则他们都happy的条件为
degree<|hw -mw|<360 - degree;<1>
degree<|hw - sw|<360 - defgree;<2>
degree<|sw - mw|<360 - degree ;<3>
解这三个不等式即可得到s的区间,把区间的最大值减去最小值就是happy的时间,把每小时每分钟的happy时间再叠加,就是总的happy时间了,再除以总时间的百分比并保留三个小数就是答案。

3、题目:

Tick and Tick

Time Limit: 2 Seconds     
Memory Limit: 65536 KB

The three hands of the clock are rotating every second and meeting each other many times everyday. Finally, they get bored of this and each of them would like to stay away from the other two. A hand is happy if it is at least D degrees from any of the rest. You are to calculate how much time in a day that all the hands are happy.

Input
The input contains many test cases. Each of them has a single line with a real number D between 0 and 120, inclusively. The input is terminated with a D of -1.

Output
For each D, print in a single line the percentage of time in a day that all of the hands are happy, accurate up to 3 decimal places.

Sample Input
0
120
90
-1

Sample Output
100.000
0.000
6.251

4、AC代码:

#include
#include
using namespace std;#define INF 0x7fffffffdouble D;struct node{ double l; double r;}s[3][2];node interval(double a,double b)//解方程D<=|a*s-b|<=360-D{ node p; if(a>0) { p.l=(D-b)/a; p.r=(360-D-b)/a; } else { p.l=(360-D-b)/a; p.r=(D-b)/a; } if(p.l>=p.r) { p.l=0; p.r=0; } if(p.l<0) p.l=0; if(p.r>60) p.r=60; return p;}node jiao(node a,node b){ node p; p.l=max(a.l,b.l); p.r=min(a.r,b.r); if(p.l>=p.r) p.l=p.r=0; return p;}double solve(int h,int m){ double a,b; /* 时针与分针的角度处理 解方程式 D<=|hw-mw|<=360-D hw=(h+m/60+s/3600)*30 mw=(m+s/60)*6 */ a=1.0/120.0-1.0/10.0; b=30*h+m/2.0-6.0*m; s[0][0]=interval(a,b); s[0][1]=interval(-a,-b); /* 时针与秒针的角度处理 解方程式 D<=|hw-sw|<=360-D hw=(h+m/60+s/3600)*30 sw=s*6 */ a=1.0/120-6.0; b=30*h+m/2.0; s[1][0]=interval(a,b); s[1][1]=interval(-a,-b); /* 分针与秒针的角度处理 解方程式 D<=|mw-sw|<=360-D mw=(m+s/3600)*30 sw=s*6 */ a=0.1-6; b=6*m; s[2][0]=interval(a,b); s[2][1]=interval(-a,-b); //两个绝对值出来的区间取并集,三个并集之间取交集 node s1; double res=0; for(int i=0;i<2;i++) for(int j=0;j<2;j++) for(int k=0;k<2;k++) { s1=jiao(jiao(s[0][i],s[1][j]),s[2][k]); res+=s1.r-s1.l; } return res;}int main(){ while(scanf("%lf",&D)!=EOF) { if(D==-1) break; double ans=0; for(int i=0;i<12;i++) { for(int j=0;j<60;j++) ans+=solve(i,j);//i小时j分钟 } printf("%.3f\n",100.0*ans/(60*60*12)); } return 0;}
其中的合并取交集参考网上代码,自己写的错了

错的代码:

#include
#include
using namespace std;#define INF 0x7fffffffdouble D;struct node{ double l; double r;}s[3][2];node interval(double a,double b)//解方程D<=|a*s-b|<=360-D{ node p; if(a>0) { p.l=(D-b)/a; p.r=(360-D-b)/a; } else { p.l=(360-D-b)/a; p.r=(D-b)/a; } if(p.l>=p.r) { p.l=0; p.r=0; } if(p.l<0) p.l=0; if(p.r>60) p.r=60; return p;}node jiao(node a,node b){ node p; p.l=max(a.l,b.l); p.r=min(a.r,b.r); if(p.l>=p.r) p.l=p.r=0; return p;}double solve(int h,int m){ double a,b; /* 时针与分针的角度处理 解方程式 D<=|hw-mw|<=360-D hw=(h+m/60+s/3600)*30 mw=(m+s/60)*6 */ a=1.0/120-1.0/10.0; b=30*h+m/2.0-6.0*m; s[0][0]=interval(a,b); s[0][1]=interval(-a,-b); /* 时针与秒针的角度处理 解方程式 D<=|hw-sw|<=360-D hw=(h+m/60+s/3600)*30 sw=s*6 */ a=1.0/120-6; b=30*h+m/2.0; s[1][0]=interval(a,b); s[1][1]=interval(-a,-b); /* 分针与秒针的角度处理 解方程式 D<=|mw-sw|<=360-D mw=(m+s/3600)*30 sw=s*6 */ a=0.1-6; b=6.0*m; s[2][0]=interval(a,b); s[2][1]=interval(-a,-b); //两个绝对值出来的区间取并集,三个并集之间取交集 node s1[20]; int k=0; for(int i=0;i<3;i++) { if((s[i][0].r>=s[i][1].l) || (s[i][1].r>=s[i][0].l) ) { s1[k].l=max(s[i][0].l,s[i][1].l); s1[k].r=max(s[i][0].r,s[i][1].r); k++; } else if(s[i][0].l>=s[i][1].l && s[i][0].r<=s[i][1].r) { s1[k]=s[i][1]; k++; } else if(s[i][1].l>=s[i][0].l && s[i][1].r<=s[i][0].r) { s1[k]=s[i][0]; k++; } else { s1[k]=s[i][0]; k++; s1[k]=s[i][1]; k++; } } node s2=s1[0]; for(int i=1;i

转载地址:http://ahddi.baihongyu.com/

你可能感兴趣的文章
SSH框架总结(框架分析+环境搭建+实例源码下载)
查看>>
js弹窗插件
查看>>
自定义 select 下拉框 多选插件
查看>>
js判断数组内是否有重复值
查看>>
js获取url链接携带的参数值
查看>>
gdb 调试core dump
查看>>
gdb debug tips
查看>>
arm linux 生成火焰图
查看>>
linux和windows内存布局验证
查看>>
linux config
查看>>
linux insmod error -1 required key invalid
查看>>
linux kconfig配置
查看>>
linux不同模块completion通信
查看>>
linux printf获得时间戳
查看>>
C语言位扩展
查看>>
linux dump_backtrace
查看>>
linux irqdebug
查看>>
git 常用命令
查看>>
linux位操作API
查看>>
uboot.lds文件分析
查看>>