两个钟表问题。

钟表问题解决思路:

以秒针的转动的角度, 计算分针与时针转过的角度。 即: 模拟现实的场景。

《1》http://bestcoder.hdu.edu.cn/contests/contest_showproblem.php?cid=607&pid=1001

#include<iostream>
#include<cstdio>
#include<cstring>
#include<vector>
using namespace std;

vector<int> v[5000000];

void Print(int x)
{
    printf("%02d:%02d:%02d\n", x/3600, x/60%60, x%60);
}

int MOD(long long x)
{
    x%=12000*360;
    if(x<0) x+=12000*360;
    if(x>12000*180) x=12000*360-x;
    return x;
}

void solve()
{
    long long h = 0;
    long long m = 0;
    for(int i=0; i<12*60*60; i+=10)
    {
        v[MOD(h-m)].push_back(i);
        h+=1000;//十秒钟, 时针走过的角度(放大的)
        m+=12000;
    }
}

int main()
{
    solve();
    int x;
    while(scanf("%d", &x)!=EOF)
    {
        for(int i=0; i<v[x].size(); i++)
            Print(v[x][i]);
    }
    return 0;
}

看到另一种精简的解法:

#include<iostream>
#include<cstdio>
#include<algorithm>
using namespace std;

int main()
{
    int x;
    while(cin>>x)
    {
        for(int h=0; h<12; ++h)
            for(int m=0; m<60; ++m)
                for(int s=0; s<60; s+=10)
                {
                    int a1=h*360000+m*6000+s*100;
                    int a2=m*72000+s*1200;
                    if(min(abs(a1-a2),360*12000-abs(a1-a2))==x)
                        printf("%02d:%02d:%02d\n",h,m,s);
                }
    }
    return 0;
}

 

 

《2》http://acm.hdu.edu.cn/showproblem.php?pid=1006

#include<cstdio>
#include<algorithm>
#include<cmath>
#include<iostream>
using namespace std;

const int MAXN = 12*60*60;
double hm, hs, ms, T_hm, T_hs, T_ms;
void init()
{
    double h, m, s;
    h=1.0/120;
    m=1.0/10;
    s=6;
    hm=m-h; hs=s-h; ms=s-m;
    T_hm=360/hm;
    T_hs=360/hs;
    T_ms=360/ms;
}

double Max(double a, double b, double c)
{
    return max(max(a, b), c);
}

double Min(double a, double b, double c)
{
    return min(min(a, b), c);
}

int main()
{
    init();
    double n;
    while(scanf("%lf", &n)!=EOF)
    {
        if(n<0) break;
        double i, j, k, a[6], p, q, ans = 0;
        a[0]=n/hm;
        a[1]=n/hs;
        a[2]=n/ms;
        a[3]=(360-n)/hm;
        a[4]=(360-n)/hs;
        a[5]=(360-n)/ms;
        for(i=0; i<=1.0*MAXN; i+=T_hm)
        {
            for(j=0; j<=1.0*MAXN; j+=T_hs)
            {
                if(j+a[1]>i+a[3]) break;
                if(i+a[0]>j+a[4]) continue;
                for(k=0; k<=1.0*MAXN; k+=T_ms)
                {
                    if(k+a[2]>i+a[3]||k+a[2]>j+a[4])break;  
                    if(i+a[0]>k+a[5]||j+a[1]>k+a[5])continue;  
                    p=Max(i+a[0],j+a[1],k+a[2]);//在这三个时间段刚好完成分离n度,所以取最大值才能保证全都分离n以上  
                    q=Min(i+a[3],j+a[4],k+a[5]);//在这三个时间段刚好完成合并n度,所以取最小值才能保证全都未合并到n以内  
                    if(q>p)  
                        ans+=q-p; 
                }
            }
        }
        printf("%.3lf\n", 100.0*ans/MAXN);
    }
    return 0;
}

 

转载于:https://www.cnblogs.com/acm1314/p/4621707.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值