×

# Wrong answer in problem POINTS

 1 Hello @all, I was trying to solve this problem: POINTS. 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 #include #include #include 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 asked 21 Jan '13, 20:43 3★kuruma 17.7k●72●143●209 accept rate: 8%

 0 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); }  This is your new comp function, right ?? Had to really search your submission. Please always give a link to the page where I can see your submitted code. Anyways, If a.x == b.x then if a.y > b.y you return true and a.y < b.y then also you return true . This is wrong. Just write if(a.x!=b.x) return a.x < b.x; return a.y > b.y;  answered 21 Jan '13, 21:47 12.4k●47●107●171 accept rate: 12% 6.7k●62●119●138
 1 if(a.x < b.x) return (a.x < b.x); else return (b.x > a.x); After these four lines in your comp function other lines are not reachable , because if the condition is true then first return statement will be executed otherwise second return statement will be executed . The checks for cases when x coordinate is equal are hence not part of your comp function . answered 21 Jan '13, 20:49 12.4k●47●107●171 accept rate: 12% Thanks for pointing this out... I have fixed it, but still get WA! (21 Jan '13, 21:08) kuruma3★
 0 Thank you very much @vineetpaliwal, I got AC after your correction :D And thank you, @anton for fixing code tags as well :D Thanks, Bruno answered 21 Jan '13, 23:03 3★kuruma 17.7k●72●143●209 accept rate: 8%
 toggle preview community wiki:
Preview

By Email:

Markdown Basics

• *italic* or _italic_
• **bold** or __bold__
• image?![alt text](/path/img.jpg "title")
• numbered list: 1. Foo 2. Bar
• to add a line break simply add two spaces to where you would like the new line to be.
• basic HTML tags are also supported
• mathemetical formulas in Latex between \$ symbol

Question tags:

×38
×15

question asked: 21 Jan '13, 20:43

question was seen: 728 times

last updated: 21 Jan '13, 23:03