Average is not Fast Enough! hdoj 1036

本文解析了一场编程比赛题目,涉及计算每队平均完成接力赛所需时间,包括处理不合格成绩及输出特定格式。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >


Average is not Fast Enough!

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 4112    Accepted Submission(s): 1660


Problem Description
A relay is a race for two or more teams of runners. Each member of a team runs one section of the race. Your task is to help to evaluate the results of a relay race.

You have to process several teams. For each team you are given a list with the running times for every section of the race. You are to compute the average time per kilometer over the whole distance. That's easy, isn't it?
So if you like the fun and challenge competing at this contest, perhaps you like a relay race, too. Students from Ulm participated e.g. at the "SOLA" relay in Zurich, Switzerland. For more information visit
http://www.sola.asvz.ethz.ch/ after the contest is over.
 

Input
The first line of the input specifies the number of sections n (赛项)followed by the total distance (总路程)of the relay d in kilometers.(d 千米) You may safely assume that 1 <= n <= 20 and 0.0 < d < 200.0. Every following line gives information about one team: the team number t (an integer, right-justified in a field of width 3)(队名:t;(t是整数,输入右对齐长度3)) is followed by the n results for each section, separated by a single space. These running times are given in the format "h:mm:ss" with integer numbers for the hours, minutes and seconds, respectively. In the special case of a runner being disqualified(被取消资格的), the running time will be denoted by "-:--:--". Finally, the data on every line is terminated by a newline character. Input is terminated by EOF.
 

Output
For each team output exactly one line giving the team's number t right aligned in a field of width 3, and the average time for this team rounded to whole seconds in the format "m:ss". If at least one of the team's runners has been disqualified, output "-" instead. Adhere to the sample output for the exact format of presentation.

每一队输出一行,队名右对齐3,并且输出该队的平均时间;

对于取消资格的队伍,只输出队名,并输出"-";


 

Sample Input
2 12.5
  5 0:23:21 0:25:01
 42 0:23:32 -:--:--
  7 0:33:20 0:41:35
 

Sample Output
  5: 3:52 min/km
 42: -
  7: 6:00 min/km
 

Source
University of Ulm Local Contest 2001

题意:算平均每千米所需要的时间;细节题目上都有;

思路:现将时间化为秒来做,再求平均值;

注意: 1、输入用到sscanf()函数来截取数值,这是常用的日后;

             2、浮点型转化为整型时精度的问题,此时可用数学函数floor()来解决;

             3、严格按照输出格式输出;

/*
sscanf()函数 :
1. 常见用法。
char buf[512] ;
sscanf("123456 ", "%s", buf);//此处buf是数组名,它的意思是将123456以%s的形式存入buf中!
printf("%s\n", buf);
结果为:123456
2. 取指定长度的字符串。如在下例中,取最大长度为4字节的字符串。
sscanf("123456 ", "%4s", buf);
printf("%s\n", buf);
结果为:1234
3. 取到指定字符为止的字符串。如在下例中,取遇到空格为止字符串。
sscanf("123456 abcdedf", "%[^ ]", buf);
printf("%s\n", buf);
结果为:123456
4. 取仅包含指定字符集的字符串。如在下例中,取仅包含1到9和小写字母的字符串。
sscanf("123456abcdedfBCDEF", "%[1-9a-z]", buf);
printf("%s\n", buf);
结果为:123456abcdedf
当输入:
sscanf("123456abcdedfBCDEF","%[1-9A-Z]",buf);
printf("%s\n",buf);
结果为:123456
5. 取到指定字符集为止的字符串。如在下例中,取遇到大写字母为止的字符串。
sscanf("123456abcdedfBCDEF", "%[^A-Z]", buf);
printf("%s\n", buf);
结果为:123456abcdedf
6、给定一个字符串
iios/12DDWDFF@122,获取 / 和 @ 之间的字符串,先将 "iios/"过滤掉,再将非'@'的一串内容送到buf中
sscanf("
iios/12DDWDFF@122", "%*[^/]/%[^@]", buf);
printf("%s\n", buf);
结果为:12DDWDFF
7、给定一个字符串"hello, world”,仅保留world。(注意:“,”之后有一空格)
sscanf(“hello, world”, "%*s%s", buf);
printf("%s\n", buf);
结果为:world
%*s表示第一个匹配到的%s被过滤掉,即hello被过滤了
如果没有空格则结果为NULL。
 8.处理2006:03:18 - 2006:04:18(‘-’两边有空格)和2006:03:18-2006:04:18(‘-’两边无空格):
前者:
char sztime1[16] = "", sztime2[16] = "";
sscanf("2006:03:18 - 2006:04:18", "%s - %s", sztime1, sztime2);

后者:
char sztime1[16] = "", sztime2[16] = "";
sscanf("2006:03:18 - 2006:04:18", "%[0-9,:]-%[0-9,:]", sztime1, sztime2);
*/

/*
函数名: floor
功 能: 返回小于或者等于指定表达式的最大整数
用 法: double floor(double x);
头文件:math.h
*/

      

#include<cstdio>
#include<string.h>
#include<math.h>
#include<stdlib.h>
double change_s(int h,int m,int s)
{
 return s=h*3600.0+m*60.0+s*1.0;
}

int main()
{
 int n;
 double d;
 while(~scanf("%d%lf",&n,&d))
 {
  int teanum,hh,mm,ss,i,p;
  double avtime,sumtime;
  char s[20];
  
   while(~scanf("%d",&teanum))
    {
     for(i=0,sumtime=0.0,p=1;i<n;i++)
    {
          scanf("%s",s);
     if(strcmp(s,"-:--:--")==0)
         {
      p=0;
         }
        else  
     {
    sscanf(s,"%d:%d:%d",&hh,&mm,&ss);
       sumtime+=change_s(hh,mm,ss);
     }
    }
   
    avtime=sumtime/d;
    int ret=floor(avtime+0.5);
  
      if(p==1)
       printf("%3d: %d:%02d min/km\n",teanum,ret/60,ret%60);
    else
      printf("%3d: -\n",teanum);
    }
 }
 return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值