zoj 2106 Tick and Tick(比较好的数学题目,代码特麻烦,注意精度)

2023-11-08 12:08

本文主要是介绍zoj 2106 Tick and Tick(比较好的数学题目,代码特麻烦,注意精度),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

1、http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=1106

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<stdio.h>
#include<algorithm>
using namespace std;
#define INF 0x7fffffff
double 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-Dhw=(h+m/60+s/3600)*30mw=(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-Dhw=(h+m/60+s/3600)*30sw=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-Dmw=(m+s/3600)*30sw=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<stdio.h>
#include<algorithm>
using namespace std;
#define INF 0x7fffffff
double 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-Dhw=(h+m/60+s/3600)*30mw=(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-Dhw=(h+m/60+s/3600)*30sw=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-Dmw=(m+s/3600)*30sw=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<k;i++){s2=jiao(s2,s1[i]);}double anss;anss=s2.r-s2.l;return anss;
}
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;
}



这篇关于zoj 2106 Tick and Tick(比较好的数学题目,代码特麻烦,注意精度)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/369748

相关文章

Python实例题之pygame开发打飞机游戏实例代码

《Python实例题之pygame开发打飞机游戏实例代码》对于python的学习者,能够写出一个飞机大战的程序代码,是不是感觉到非常的开心,:本文主要介绍Python实例题之pygame开发打飞机... 目录题目pygame-aircraft-game使用 Pygame 开发的打飞机游戏脚本代码解释初始化部

Java中Map.Entry()含义及方法使用代码

《Java中Map.Entry()含义及方法使用代码》:本文主要介绍Java中Map.Entry()含义及方法使用的相关资料,Map.Entry是Java中Map的静态内部接口,用于表示键值对,其... 目录前言 Map.Entry作用核心方法常见使用场景1. 遍历 Map 的所有键值对2. 直接修改 Ma

深入解析 Java Future 类及代码示例

《深入解析JavaFuture类及代码示例》JavaFuture是java.util.concurrent包中用于表示异步计算结果的核心接口,下面给大家介绍JavaFuture类及实例代码,感兴... 目录一、Future 类概述二、核心工作机制代码示例执行流程2. 状态机模型3. 核心方法解析行为总结:三

python获取cmd环境变量值的实现代码

《python获取cmd环境变量值的实现代码》:本文主要介绍在Python中获取命令行(cmd)环境变量的值,可以使用标准库中的os模块,需要的朋友可以参考下... 前言全局说明在执行py过程中,总要使用到系统环境变量一、说明1.1 环境:Windows 11 家庭版 24H2 26100.4061

pandas实现数据concat拼接的示例代码

《pandas实现数据concat拼接的示例代码》pandas.concat用于合并DataFrame或Series,本文主要介绍了pandas实现数据concat拼接的示例代码,具有一定的参考价值,... 目录语法示例:使用pandas.concat合并数据默认的concat:参数axis=0,join=

C#代码实现解析WTGPS和BD数据

《C#代码实现解析WTGPS和BD数据》在现代的导航与定位应用中,准确解析GPS和北斗(BD)等卫星定位数据至关重要,本文将使用C#语言实现解析WTGPS和BD数据,需要的可以了解下... 目录一、代码结构概览1. 核心解析方法2. 位置信息解析3. 经纬度转换方法4. 日期和时间戳解析5. 辅助方法二、L

Python使用Code2flow将代码转化为流程图的操作教程

《Python使用Code2flow将代码转化为流程图的操作教程》Code2flow是一款开源工具,能够将代码自动转换为流程图,该工具对于代码审查、调试和理解大型代码库非常有用,在这篇博客中,我们将深... 目录引言1nVflRA、为什么选择 Code2flow?2、安装 Code2flow3、基本功能演示

IIS 7.0 及更高版本中的 FTP 状态代码

《IIS7.0及更高版本中的FTP状态代码》本文介绍IIS7.0中的FTP状态代码,方便大家在使用iis中发现ftp的问题... 简介尝试使用 FTP 访问运行 Internet Information Services (IIS) 7.0 或更高版本的服务器上的内容时,IIS 将返回指示响应状态的数字代

MySQL 添加索引5种方式示例详解(实用sql代码)

《MySQL添加索引5种方式示例详解(实用sql代码)》在MySQL数据库中添加索引可以帮助提高查询性能,尤其是在数据量大的表中,下面给大家分享MySQL添加索引5种方式示例详解(实用sql代码),... 在mysql数据库中添加索引可以帮助提高查询性能,尤其是在数据量大的表中。索引可以在创建表时定义,也可

使用C#删除Excel表格中的重复行数据的代码详解

《使用C#删除Excel表格中的重复行数据的代码详解》重复行是指在Excel表格中完全相同的多行数据,删除这些重复行至关重要,因为它们不仅会干扰数据分析,还可能导致错误的决策和结论,所以本文给大家介绍... 目录简介使用工具C# 删除Excel工作表中的重复行语法工作原理实现代码C# 删除指定Excel单元