Hello @all,
I was trying to solve this problem: [POINTS][1].
I think Im accounting for all the I/O format specifications and problem statement… However, Im getting WA… Can you help me out?
Here’s my code:
#include <stdio.h>
#include <iostream>
#include <algorithm>
#include <cmath>
using namespace std;
struct Point
{
int x;
int y;
};
double dist(Point p1, Point p2)
{
return sqrt( pow((p1.x-p2.x),2) + pow((p1.y-p2.y),2));
}
bool comp(Point a, Point b)
{
if(a.x < b.x)
return (a.x < b.x);
else if(b.x < a.x)
return (b.x > a.x);
else if(a.x==b.x and a.y > b.y)
return (a.y > b.y);
else if(a.x==b.x and b.y > a.y)
return (b.y > a.y);
}
int main()
{
int t;
scanf("%d", &t);
for(int i = 0; i < t; i++)
{
cout << endl;
int num;
scanf("%d", &num);
Point array[num];
for(int j = 0; j < num; j++)
{
int abx, ord;
scanf("%d %d", &abx, &ord);
array[j].x=abx;
array[j].y=ord;
}
double distance = 0.00;
sort(array,array+num,comp);
for(int i = 0; i < num-1; i++)
{
distance += dist(array[i],array[i+1]);
}
cout << endl;
printf("%.2lf\n", distance);
}
return 0;
}
Thanks in advance,
Bruno
[1]: http://www.codechef.com/problems/POINTS