lunes, 24 de octubre de 2011

10281 - Average Speed - UVA

#include <iostream>
#include <cstdio>
#include <sstream>
#include <cstdlib>
#define db(a) cout << #a << " = " << a << endl;
using namespace std;
int seconds(int h, int m, int s){
 return h * 3600 + m * 60 + s;
}
int main(){
 string linea, tiempo;
 int h, m, s;
 float recorrido = 0;
 stringstream ss;
 /*freopen("in.in", "r", stdin);
 freopen("ou.out", "w", stdout);*/
 int current_time = 0;
 float velocidad_ini = 0;
 bool primero = true;
 while(getline(cin, linea)){
  ss.clear();
  ss << linea;
  bool first = true;
  while(ss >> linea){
   if(first){
    sscanf(linea.c_str(), "%d:%d:%d", &h, &m, &s);
    tiempo = linea;
    first = false;
   }
   else first = true;
  }
  if(first){
   if(primero){ 
    recorrido = 0; 
    primero = false;
    current_time = seconds(h, m, s);
   }
   else {
    recorrido += (seconds(h, m, s) - current_time) * velocidad_ini / 3600.;
    current_time = seconds(h , m, s);
   }
   velocidad_ini = atoi(linea.c_str());
  }
  else{
   int tiempo_reporte = seconds(h, m, s) - current_time;
   float distancia = velocidad_ini * tiempo_reporte / 3600.;
   distancia += recorrido;
   printf("%s %.2f km\n", tiempo.c_str(), distancia);
  }
 }
 
 return 0;
}

No hay comentarios:

Publicar un comentario