u011042985 于 2016.03.20 10:28 提问

#include
#include
using namespace std;

int round(double sum, int num) {
if(sum>0)
return (int)(sum/num + 0.5);
else if(sum == 0) return 0;
else return (int)(sum/num - 0.5);
}

bool isexit(int* x, int y, int num) {
for(int i=0; i<num; i++) {
if(x[i] == y)
return true;
}
return false;
}

int main() {
int num, i=0, var;
int *x;
int *y;
double sum = 0;
cin>>num;
x = (int *) malloc(num * sizeof(int));
y = (int *) malloc(num * sizeof(int));
while(i cin>>x[i]>>y[i];
sum += y[i++];
}

``````var = round(sum, num);
sum = 0;
for(i=0; i<num; i++) {
sum += abs(y[i] - var);
}
int xmove, ymove;
for(i=0; i<num; i++) {
for(int j=0; j<num&&j!=i ;j++)
if(x[j]==x[i]) {
xmove = x[j]-1;
while(isexit(x, xmove, num)) {
--xmove;
}
ymove = x[j]+1;
while(isexit(x, ymove, num)) {
++ymove;
}
if(x[j] - xmove > ymove - x[j]) {
sum += abs(ymove - x[j]);
x[j] = ymove;
}
else {
sum += abs(x[j] - xmove);
x[j] = xmove;
}
}
}
cout<<sum<<endl;
free(x);
free(y);
return 0;
``````

}

1个回答

huatian5   2016.03.20 11:28

u011042985 恩，就是编译器的语法