虽然我AC了,但是我还是不大清楚是为什么。。。。 好吧,我先留着,以后再好好看看。。。 #include <cstdio> #include <algorithm> using namespace std; const int MAX = 100010; typedef struct _Node { int x , y; }Node; Node data[MAX]; int n; bool cmp1( const Node & left , const Node & right ) { if( left.x != right.x ) return left.x < right.x ; else return left.y < right.y ; } bool cmp2( const Node & left , const Node & right ) { if( left.y != right.y ) return left.y < right.y ; else return left.x < right.x ; } int getrow() { sort( &data[0] , &data[n] , cmp1 ); int sum = 0 ; for( int i = 0 ; i != n ; i += 2 ) sum += data[i+1].y - data[i].y ; return sum; } int getcol() { sort( &data[0] , &data[n] , cmp2 ); int sum = 0 ; for( int i = 0 ; i != n ; i += 2 ) sum += data[i+1].x - data[i].x ; return sum; } int main() { while( scanf("%d",&n) , n != 0 ) { for( int i = 0 ; i != n ; ++i ) scanf("%d%d",&data[i].x , &data[i].y ); printf("The length of the fence will be %d units./n",getrow() + getcol() ); } return 0; }