zoukankan      html  css  js  c++  java
  • 【推公式】UVa 10995

    1A~,但后来看人家的代码好像又写臭了,T^T...

    Problem A: Educational journey

    The University of Calgary team qualified for the 28th ACM International Collegiate Programming Contest World Finals in Prague, Czech Republic. Just by using the initials of team members they got a very cunning team name: ACM (Alecs, Celly andMonny). In order to prepare for the contest, they have decided to travel to Edmonton to learn the tricks of trade from Dilbert, Alberta-wide famous top-coder.

    Due to a horrible miscommunication which is as welcome as a plague among such teams, AC and M drive from Calgary to Edmonton in separate cars. To make things worse, there was also a miscommunication with D, who being always so helpful, decides to go to Calgary in order to save the team a trip to the far, freezing North. All this happens on the same day and each car travels at a constant (but not necessarily the same) speed on the famous Alberta #2.

    A passed C and M at time t1 and t2, respectively, and met D at time t3D met Cand M at times t4 and t5, respectively. The question is: at what time time did Cpass M?

    The input is a sequence of lines, each containing times t1t2t3t4 and t5, separated by white space. All times are distinct and given in increasing order. Each time is given in the hh:mm:ss format on the 24-hour clock. A line containing -1 terminates the input.

    For each line of input produce one line of output giving the time when C passed M in the same format as input, rounding the seconds in the standard way.

    Sample input

    10:00:00 11:00:00 12:00:00 13:00:00 14:00:00
    10:20:00 10:58:00 14:32:00 14:59:00 16:00:00
    10:20:00 12:58:00 14:32:00 14:59:00 16:00:00
    08:00:00 09:00:00 10:00:00 12:00:00 14:00:00
    -1
    

    Output for sample input

    12:00:00
    11:16:54
    13:37:32
    10:40:00

    题意:就是t1,t2,t3,t4,t5分别代表A碰见C,A碰见M,A碰见D,D碰见C,D碰见M(很明显C后来赶超了M),且保证时间依次递增,问C与M相遇的时间;每个人运动的速率为常数且彼此不一定相等。
    分析:以D的位置为基准,AC相遇时二者距D的距离相等,用二者分别到达D点的时间差之比求出二者速度之比;同理求出AM速度之比;设CM相遇时时间为tmp,则推出tmp与速度间的表达式。注意计算时需要换成秒计算,且注意数的范围,需用long long。比例运算时可能出现小数,注意四舍五入等精度问题。

    臭代码拉出来丢人一下:
     1 #include<iostream>
     2 #include<cstdio>
     3 #include<cstring>
     4 #include<cmath>
     5 #include<cstring>
     6 #include<string>
     7 #define error 1e-8
     8 using namespace std;
     9 typedef long long LL;
    10 const int maxn = 15;
    11 string tt[10];
    12 struct Time
    13 {
    14     int h, m, s;
    15 }t[10];
    16 LL Sub(Time b, Time a)
    17 {
    18     int hh, mm, ss;
    19     if(b.s >= a.s) ss = b.s-a.s;
    20     else
    21     {
    22         ss = b.s+60-a.s;
    23         b.m--;
    24     }
    25     if(b.m >= a.m) mm = b.m-a.m;
    26     else
    27     {
    28         mm = b.m+60-a.m;
    29         b.h--;
    30     }
    31     hh = b.h-a.h;
    32     return ss+mm*60+hh*3600;
    33 }
    34 LL turn_to_LL(Time x)
    35 {
    36     return x.s+x.m*60+x.h*3600;
    37 }
    38 Time turn_to_time(LL x)
    39 {
    40     Time tmp;
    41     tmp.h = x/3600; x -= tmp.h*3600;
    42     tmp.m = x/60;   x -= tmp.m*60;
    43     tmp.s = x;
    44     return tmp;
    45 }
    46 int trans(string s)
    47 {
    48     return 10*(s[0]-'0')+(s[1]-'0');
    49 }
    50 int main()
    51 {
    52     //freopen("in.txt", "r", stdin);
    53     while(cin >> tt[0])
    54     {
    55         if(tt[0] == "-1") break;
    56         for(int i = 1; i < 5; i++) cin >> tt[i];
    57         string s;
    58         for(int i = 0; i < 5; i++)
    59         {
    60             int pos1 = tt[i].find(':'), pos2 = tt[i].find_last_of(':');
    61             s = tt[i].substr(0, pos1);
    62             t[i].h = trans(s);
    63             s = tt[i].substr(pos1+1, pos2-pos1-1);
    64             t[i].m = trans(s);
    65             s = tt[i].substr(pos2+1);
    66             t[i].s = trans(s);
    67         }
    68         LL n = Sub(t[4],t[1])*Sub(t[2],t[0]);
    69         LL m = Sub(t[2],t[1])*Sub(t[3],t[0]);
    70 
    71         LL tmp = LL(double(m*turn_to_LL(t[4])-n*turn_to_LL(t[3]))/(m-n) + 0.5);
    72         //cout << double(m*turn_to_LL(t[4])-n*turn_to_LL(t[3]))/(m-n) << endl;
    73         Time res = turn_to_time(tmp);
    74         printf("%.2d:%.2d:%.2d
    ", res.h, res.m, res.s);
    75     }
    76     return 0;
    77 }
    View Code
     
  • 相关阅读:
    对焦过程中消除摩尔纹
    Python3.x:Linux下安装python3.6
    Python3.x:Linux下退出python命令行
    Python3.x:ConfigParser模块的使用
    Python3.x:SQLAlchemy操作数据库
    Python3.x:遍历select下拉框获取value值
    Python3.x:Selenium中的webdriver进行页面元素定位
    Python3.x:selenium获取iframe内嵌页面的源码
    Python3.x:Selenium+PhantomJS爬取带Ajax、Js的网页
    Python3.x:将数据下载到xls时候用xml格式保存一份读取内容
  • 原文地址:https://www.cnblogs.com/LLGemini/p/4328414.html
Copyright © 2011-2022 走看看