杭电OJ——1006 Tick and Tick

部署运行你感兴趣的模型镜像

Tick and Tick

Time Limit: 2000/1000 MS (Java/Others)Memory Limit: 65536/32768 K (Java/Others)


Problem Description
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

Author
PAN, Minghao

Source

Recommend
JGShining


代码参考如下:

/*****************************
12h=43200s;
v=6 度/秒(秒针速度)
v'=1/10 度/秒 (分针速度)
v''=1/120 度/秒 (时针速度)
v1=59/10 度/秒(秒针和分针速度差)
v2=719/120 度/秒(秒针和时针速度差)
v3=11/120 度/秒(分针和时针速度差)
******************************/
#include<iostream>
#include<cstdio>
using namespace std;

inline double max(double a,double b,double c) 
{ 
    a= a > b ? a: b; 
    a= a > c ? a: c; 
    return a; 
} 

inline double min(double a,double b,double c) 
{ 
    a= a < b ? a: b; 
    a= a < c ? a: c; 
    return a; 
} 

int main()
{
    int i,j;
    double dsm[1444],dsh[1444],dmh[26];//用于记录表的指针距离d的起始与终结时间
    double d,sum,x,y;//注意精度问题,d应为double类型,不应为int类型,否则会出错
    double sm,mh,sh;
    int smf[2],mhf[2],shf[2];
    sm=3600.0000/59;//秒针与分针重合一次所需时间
    sh=43200.0000/719;//秒针与时针重合一次所需时间
    mh=43200.0000/11;//时针与分针重合一次所需时间
    while(cin>>d && d!=-1)
    {
        sum=0;x=0;y=0;
        dsm[0]=(10*d)/59;//记录第一次秒针与分针距离d的时间
        dsh[0]=(120*d)/719;//记录第一次秒针与时针距离d的时间
        dmh[0]=(120*d)/11;//记录第一次分针与时针距离d的时间
        dsm[1]=sm-dsm[0];//记录第一次秒针与时针距离开始小于d的时间
        dsh[1]=sh-dsh[0];//记录第一次秒针与时针距离开始小于d的时间
        dmh[1]=mh-dmh[0];//记录第一次秒针与时针距离开始小于d的时间

        for(i=2,j=3;;i+=2,j+=2)
        {
            dsm[i]=dsm[i-2]+sm;//下一次距离大于等于d的时间
            dsm[j]=dsm[j-2]+sm;//下一次距离小于d的时间
            if(dsm[i]>43200 && dsm[j]>43200)  break;
        }//分别记录下每一次距离d和开始小于d的时间

         for(i=2,j=3;;i+=2,j+=2)
        {
            dsh[i]=dsh[i-2]+sh;//下一次距离大于等于d的时间
            dsh[j]=dsh[j-2]+sh;//下一次距离小于d的时间
            if(dsh[i]>43200 && dsh[j]>43200)  break;
        }//分别记录下每一次距离d和开始小于d的时间

        for(i=2,j=3;;i+=2,j+=2)
        {
            dmh[i]=dmh[i-2]+mh;//下一次距离大于等于d的时间
            dmh[j]=dmh[j-2]+mh;//下一次距离小于d的时间
            if(dmh[i]>43200 && dmh[j]>43200)  break;
        }//分别记录下每一次距离d和开始小于d的时间

        smf[0]=mhf[0]=shf[0]=0;
        smf[1]=mhf[1]=shf[1]=1;//初始化,以第一次为基点
        while(y<=43200 && x<=43200)
        {
            x=max(dsm[smf[0]],dsh[shf[0]],dmh[mhf[0]]);//起点找大者
            y=min(dsm[smf[1]],dsh[shf[1]],dmh[mhf[1]]);//终点找小者
            if(x<y) sum+=y-x;//一段一段的累加满足的时间
            if(y==dsm[smf[1]]) { smf[0]+=2; smf[1]+=2; }//转向下一次
            if(y==dsh[shf[1]]) { shf[0]+=2; shf[1]+=2; }//转向下一次
            if(y==dmh[mhf[1]]) { mhf[0]+=2; mhf[1]+=2; }//转向下一次
        }
        printf("%.3f\n",sum/432); //实际上只计算了12个小时,不过比例可以替代一天
    }
    return 0;
}

您可能感兴趣的与本文相关的镜像

Linly-Talker

Linly-Talker

AI应用

Linly-Talker是一款创新的数字人对话系统,它融合了最新的人工智能技术,包括大型语言模型(LLM)、自动语音识别(ASR)、文本到语音转换(TTS)和语音克隆技术

### 杭电OJ 1006 题解与解题思路 杭电OJ 1006题目要求计算一个整数序列中连续子序列的最大和。这是典型的动态规划问题,也可以使用Kadane算法高效求解。 #### 问题描述 给定一个整数序列 $ a[1], a[2], ..., a[n] $,求出其连续子序列的最大和。例如,输入序列 $ (6, -1, 5, 4, -7) $ 的最大子序列和为 $ 6 + (-1) + 5 + 4 = 14 $ [^4]。 #### 解题思路 1. **Kadane算法**: - 初始化两个变量:`current_sum` 用于存储当前子序列的和,`max_sum` 用于存储最大子序列和。 - 遍历数组,将当前元素加入 `current_sum`。 - 如果 `current_sum` 大于 `max_sum`,则更新 `max_sum`。 - 如果 `current_sum` 小于0,则将其重置为0,重新开始计算新的子序列。 2. **动态规划方法**: - 定义状态转移方程:`dp[i] = max(dp[i-1] + a[i], a[i])`,其中 `dp[i]` 表示以第 `i` 个元素结尾的最大子序列和。 - 最终答案为 `dp` 数组中的最大值。 #### 示例代码 以下是使用 Kadane 算法实现的 C++ 代码: ```cpp #include <iostream> #include <vector> #include <climits> using namespace std; int main() { int T; cin >> T; while (T--) { int N; cin >> N; vector<int> a(N); for (int i = 0; i < N; ++i) { cin >> a[i]; } int max_sum = INT_MIN; int current_sum = 0; for (int num : a) { current_sum = max(num, current_sum + num); max_sum = max(max_sum, current_sum); } cout << max_sum << endl; } return 0; } ``` #### 时间与空间复杂度 - **时间复杂度**:$ O(n) $,其中 $ n $ 是序列的长度。 - **空间复杂度**:$ O(1) $,使用常数额外空间。
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值