1 条题解

  • 0
    @ 2025-9-10 9:00:39

    C :

    #include <stdio.h>
    #include <assert.h>
    #include <stdlib.h>
    #include <math.h>
    
    /* how close to call equal */
    #define EPSILON 1E-8
    
    typedef struct {
      double x, y;
    } Point;
    
    /* returns 1 if intersect at a point, 0 if not, -1 if the lines coincide */
    int intersect_line(Point a, Point b, Point c, Point d, Point *p) {
      Point t;
      double r, s, denom, num1, num2;
      assert((a.x != b.x || a.y != b.y) && (c.x != d.x || c.y != d.y));
      num1 = (a.y - c.y)*(d.x - c.x) - (a.x - c.x)*(d.y - c.y);
      num2 = (a.y - c.y)*(b.x - a.x) - (a.x - c.x)*(b.y - a.y);
      denom = (b.x - a.x)*(d.y - c.y) - (b.y - a.y)*(d.x - c.x);
      if (fabs(denom) >= EPSILON) {
        r = num1 / denom;
        s = num2 / denom;
        if (0-EPSILON <= r && r <= 1+EPSILON && 
            0-EPSILON <= s && s <= 1+EPSILON) {
          p->x = a.x + r*(b.x - a.x);
          p->y = a.y + r*(b.y - a.y);
          return 1;
        } else return 0;
      } else {
        if (fabs(num1) >= EPSILON) {
          return 0;
        } else {
          if (a.x > b.x || (a.x == b.x && a.y > b.y)) { t = a; a = b; b = t; }
          if (c.x > d.x || (c.x == d.x && c.y > d.y)) { t = c; c = d; d = t; }
          if (a.x == b.x) { /* vertical lines */
            if (b.y == c.y) { *p = b; return 1;
            } else if (a.y == d.y) { *p = a; return 1;
            } else if (b.y < c.y || d.y < a.y) { return 0;
            } else return -1;
          } else {
            if (b.x == c.x) { *p = b; return 1;
            } else if (a.x == d.x) { *p = a; return 1; 
    	} else if (b.x < c.x || d.x < a.x) return 0;
            else return -1;
          }
          return -1;
        }
      }
    }
    
    int main () {
      int h, m, s;
      double x[6];
      while (1) {
        for (int i = 1; i < 6; i++) {
          if (scanf("%d:%d:%d",&h,&m,&s) != 3) return 0;
          x[i] = h*3600+m*60+s;
        }
        assert(x[1] < x[2]);
        assert(x[2] < x[3]);
        assert(x[3] < x[4]);
        assert(x[4] < x[5]);
        double y0, y1, y2, y3, y4, y5;
        y0 = y5 = 10000;
        y3 = y5+(x[5]-x[3])/x[5]*(y0-y5);
        y4 = y5+(x[5]-x[4])/x[5]*(y0-y5);
        y2 = y3*x[2]/x[3];
        y1 = y3*x[1]/x[3];
        Point a, b, c, d, r;
        a.x=x[1]; a.y=y1; b.x=x[4]; b.y=y4; c.x=x[2]; c.y=y2; d.x=x[5]; d.y=y5;
        int res = intersect_line(a, b, c, d, &r);
        assert (res == 1);
        char buf[100];
        sprintf(buf, "%.0f",r.x); sscanf(buf,"%d",&s);
        h = s/3600; s %= 3600; m = s/60; s %= 60;
        printf("%02d:%02d:%02d\n",h,m,s);
      }
      return 0;
    }
    
    

    C++ :

    #include<iostream>
    #include<cstdio>
    #include<cstring>
    #include<cmath>
    #include<cstring>
    #include<string>
    #define error 1e-8
    using namespace std;
    typedef long long LL;
    const int maxn = 15;
    string tt[10];
    struct Time
    {
        int h, m, s;
    }t[10];
    LL Sub(Time b, Time a)
    {
        int hh, mm, ss;
        if(b.s >= a.s) ss = b.s-a.s;
        else
        {
            ss = b.s+60-a.s;
            b.m--;
        }
        if(b.m >= a.m) mm = b.m-a.m;
        else
        {
            mm = b.m+60-a.m;
            b.h--;
        }
        hh = b.h-a.h;
        return ss+mm*60+hh*3600;
    }
    LL turn_to_LL(Time x)
    {
        return x.s+x.m*60+x.h*3600;
    }
    Time turn_to_time(LL x)
    {
        Time tmp;
        tmp.h = x/3600; x -= tmp.h*3600;
        tmp.m = x/60;   x -= tmp.m*60;
        tmp.s = x;
        return tmp;
    }
    int trans(string s)
    {
        return 10*(s[0]-'0')+(s[1]-'0');
    }
    int main()
    {
        //freopen("in.txt", "r", stdin);
        while(cin >> tt[0])
        {
            if(tt[0] == "-1") break;
            for(int i = 1; i < 5; i++) cin >> tt[i];
            string s;
            for(int i = 0; i < 5; i++)
            {
                int pos1 = tt[i].find(':'), pos2 = tt[i].find_last_of(':');
                s = tt[i].substr(0, pos1);
                t[i].h = trans(s);
                s = tt[i].substr(pos1+1, pos2-pos1-1);
                t[i].m = trans(s);
                s = tt[i].substr(pos2+1);
                t[i].s = trans(s);
            }
            LL n = Sub(t[4],t[1])*Sub(t[2],t[0]);
            LL m = Sub(t[2],t[1])*Sub(t[3],t[0]);
    
            LL tmp = LL(double(m*turn_to_LL(t[4])-n*turn_to_LL(t[3]))/(m-n) + 0.5);
            //cout << double(m*turn_to_LL(t[4])-n*turn_to_LL(t[3]))/(m-n) << endl;
            Time res = turn_to_time(tmp);
            printf("%.2d:%.2d:%.2d\n", res.h, res.m, res.s);
        }
        return 0;
    }
    
    • 1

    信息

    ID
    3629
    时间
    1000ms
    内存
    128MiB
    难度
    (无)
    标签
    递交数
    0
    已通过
    0
    上传者