#include <stdio.h> #include <math.h> #include <string.h> #include <stdlib.h> #include <iostream> #include <sstream> #include <algorithm> #include <set> #include <queue> #include <stack> #include <map> using namespace std; typedef long long LL; const int inf=0x3f3f3f3f; const double pi= acos(-1.0); const double esp=1e-6; const int maxn=21010; { int n,m,i,j; int sum; int x[30],y[30]; scanf("%d",&n); while(n--) { memset(x,0,sizeof(x)); memset(y,0,sizeof(y)); sum=0; scanf("%d",&m); for(i=0; i<m; i++) scanf("%d %d",&x[i],&y[i]); sort(x,x+m); sort(y,y+m); for(i=0; i<m/2; i++) { sum+=(x[m-i-1]-x[i])+(y[m-i-1]-y[i]); } printf("%d\n",sum); } return 0; } ``` 学会使用memset和sort,自己试着用c重写了一次```c #include<stdio.h> #include<stdlib.h> int cmpfunc (const void * a, const void * b); int cmpfunc (const void * a, const void * b) { return ( *(int*)a - *(int*)b ); } int main() { int n; scanf("%d",&n); for(int i=0;i<n;i++) { int changdu,sum=0; int x[30],y[30]; scanf("%d",&changdu); for(int j=0;j<changdu;j++) { scanf("%d %d",x[j],y[j]); } qsort(x, changdu, sizeof(int), cmpfunc); qsort(y, changdu, sizeof(int), cmpfunc); for(int k=0; k<changdu/2; i++) { sum+=(x[changdu-k-1]-x[k])+(y[changdu-k-1]-y[k]); } printf("%d\n",sum); } return 0; }