Monday, December 30, 2013

typedef struct, pointers, qsort and codechef POINTS - "the way to a friends house is never too long" solution

codechef POINTS - "the way to a friends house is never too long": http://www.codechef.com/problems/POINTS

codechef POINTS - "the way to a friends house is never too long" solution: http://ideone.com/zUh4EH

#include <iostream>
#include <cstdio>
#include <cmath>
#include <cstdlib>
using namespace std;
int arr[100005][2];

typedef struct A { long long x, y; }; 

int comp(const void *a, const void *b) {
A *i1, *i2;
i1=(A*)a; i2=(A*)b;
if(i1->x > i2->x) return 1;
if(i1->x == i2->x && i1->y < i2->y) return 1;
return 0;
}

int main() {
// your code goes here
int t, n, i;
double dis;
A arr[100005];
scanf("%d", &t);
while(t--) {
scanf("%d", &n); 
for(i=0; i<n; i++) cin>>arr[i].x>>arr[i].y;
qsort(arr, n, sizeof(A), comp);
dis=0;
for(i=1; i<n; i++) dis=dis+sqrt((arr[i].x-arr[i-1].x)*(arr[i].x-arr[i-1].x)+(arr[i].y-arr[i-1].y)*(arr[i].y-arr[i-1].y));
printf("%.2f\n", dis);
}
return 0;
}

typedef struct, pointers, qsort and codechef POINTS - "the way to a friends house is never too long" guidance:

in this problem a newbie coder has to look at sorting(qsort here): http://stackoverflow.com/questions/1787996/c-library-function-to-do-sort

here is also some qsort c++ explained: http://www.cplusplus.com/reference/cstdlib/qsort/

c pointers in wiki: http://en.wikipedia.org/wiki/Pointer_(computer_programming)

typedef struct c: http://en.wikipedia.org/wiki/Struct_(C_programming_language)

and after glancing all the links above, the coder would understand the above problems solutions

No comments:

Post a Comment