最后的输出竟然要这样子四舍五入,晕倒…… 害我WA了2次,代码走读了n遍 if (speedY > 0) offset = 0.5; else offset = -0.5; speedY = ((long)(speedY*100.0+offset)) / 100.0; printf("%.2f %.2f/n", speedX, speedY); 另外,poj这两天是不是挂了,一直上不去 全部代码: // zju1358 Moving Object Recognition #include <cstdio> #include <cstring> #define MAX_N 256 char matrix[MAX_N+1][MAX_N+1]; char mark[MAX_N][MAX_N]; struct POS { long y; long x; POS(long in_y, long in_x) { y = in_y; x = in_x; } POS() {} POS operator+ (const POS& right) { return POS(y+right.y, x+right.x); } inline bool isWhite() { return ('x' == matrix[y][x]); } inline bool valid(long ROW, long COL) { return ((0<=x) && (x<COL) && (0<=y) && (y<ROW)); } inline char isOccupy() { return mark[y][x]; } inline void occupy() { mark[y][x] = 1; } }; POS moves[4] = {POS(1,0), POS(0,-1), POS(-1,0), POS(0,1)}; POS queue[MAX_N]; long queHead, queTail; double centerX[MAX_N]; double centerY[MAX_N]; int main() { long COL, x, ROW, y; long nPic; while (2 == scanf("%ld%ld", &COL, &ROW)) { if (0==COL || 0==ROW) break; nPic = 0; // each picture // do { memset(mark, 0, MAX_N*MAX_N*sizeof(mark[0][0])); for (y=0; y<ROW; y++) scanf("%s", matrix[y]); // wfs // long nMaxPoint = 0; long sumMaxX=0, sumMaxY=0; long nPoint; long sumX, sumY; for (y=0; y<ROW; y++) { for (x=0; x<COL; x++) { POS pos(y,x); if (!pos.isOccupy() && pos.isWhite()) { nPoint = sumX = sumY = 0; queHead = queTail = 0; queue[0] = pos; pos.occupy(); while (queHead <= queTail) { POS posCur = queue[queHead++]; nPoint++; sumX += posCur.x; sumY += posCur.y; for (long m=0; m<4; m++) { POS posNext = posCur + moves[m]; if (posNext.valid(ROW, COL) && !posNext.isOccupy() && posNext.isWhite()) { queue[++queTail] = posNext; posNext.occupy(); } } } if (nPoint > nMaxPoint) { nMaxPoint = nPoint; sumMaxX = sumX; sumMaxY = sumY; } } } } // wfs // centerX[nPic] = (double)sumMaxX / (double)nMaxPoint; centerY[nPic] = (double)sumMaxY / (double)nMaxPoint; nPic++; scanf("%s", matrix[MAX_N]); } while ('-' == matrix[MAX_N][0]); // each picture // // calculate // double speedX=0.0, speedY=0.0; long T = nPic / 2; long i; for (i=0; i<T; i++) { speedX += centerX[i+T] - centerX[i]; speedY += centerY[i+T] - centerY[i]; } speedX = speedX / T / T; speedY = speedY / T / T; double offset; if (speedX > 0) offset = 0.5; else offset = -0.5; speedX = ((long)(speedX*100.0+offset)) / 100.0; if (speedY > 0) offset = 0.5; else offset = -0.5; speedY = ((long)(speedY*100.0+offset)) / 100.0; // calculate // printf("%.2f %.2f/n", speedX, speedY); } return 0; }