Advertisement
JosepRivaille

P28118: Representant comercial

Apr 6th, 2015
932
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.81 KB | None | 0 0
  1. #include <iostream>
  2. #include <string>
  3. #include <cmath>
  4. using namespace std;
  5.  
  6.  
  7. double distancia(double x1, double y1, double x2, double y2) {
  8.     double dist, x, y;
  9.     x = x1 - x2;
  10.     y = y1 - y2;
  11.     dist = sqrt(x*x + y*y);
  12.     return dist;
  13. }
  14.  
  15. //Pre: Llegeix diferents punts d'un trajecte
  16. //Post: Escriu la distància del recorregut
  17. int main() {
  18.     cout.setf(ios::fixed);
  19.     cout.precision(4);
  20.     string ruta;
  21.     double x, y, aux1, aux2;
  22.     double x1, x2, y1, y2;
  23.     while (cin >> ruta) {
  24.         bool fiseq = false;
  25.         double t = 0;
  26.         cin >> x >> y;
  27.         aux1 = x;
  28.         aux2 = y;
  29.         x1 = x;
  30.         y1 = y;
  31.         while (not fiseq && cin >> x >> y) {
  32.             x2 = x1;
  33.             y2 = y1;
  34.             x1 = x;
  35.             y1 = y;
  36.             t = t + distancia(x1, y1, x2, y2);
  37.             if (x == aux1 && y == aux2) fiseq = true;
  38.         }
  39.         cout << "Trajecte " << ruta << ": " << t << endl;
  40.     }
  41. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement