-4

此代码导致分段错误,但我不知道为什么。它必须在 main 函数的第一个 while 循环中。有人可以告诉我,是什么导致了分段错误或错误在哪里?输入文件为:
1 2
2 3
3 4
4 5

#include <iostream>
#include <fstream>
#include <algorithm>
#include <list>

using namespace std;

const int maxRec = 4;
int area;

struct Rectangle {
  int h, w, area;
  Rectangle(int i = 0, int j = 0) {
    h = j;
    w = i;
    area = h*w;
  }

  void swap() {
    h ^= w ^= h ^= w;
  }

  void wh() {
    if(h < w) return;
    swap();
  }

  friend bool operator < (const Rectangle a, const Rectangle b) {
    return (a.w < b.w);
  }

  friend bool operator == (const Rectangle a, const Rectangle b) {
    return (a.h == b.h) && (a.w == b.w);
  }
};

list<Rectangle> square;

Rectangle first(const Rectangle rect[maxRec]) {
  int i = 0, j = 0;
  for(int a = 0; a < maxRec; a++) {
    i += rect[i].w;
    j = max(j, rect[a].h);
  }
  return Rectangle(i,j);
}

Rectangle second(const Rectangle rect[maxRec]) {
  int i = 0, j = 0;
  for(int a = 1; a < maxRec; a++) {
    i += rect[a].w;
    j = max(j, rect[i].h);
  }
  return Rectangle(max(i, rect[0].w),j+rect[0].h);
}

Rectangle third(const Rectangle rect[maxRec]) {
  int i = 0, j = 0;
  for(int a = 0; a < 2; a++) {
    i += rect[a].w;
    j += rect[a].h;
  }
  return Rectangle(rect[3].w + max(i,rect[2].w), max(j+rect[2].h,rect[3].h));
}

Rectangle fourth(const Rectangle rect[maxRec]) {
  int i = 0, j = 0;
  for(int a = 0; a < 2; a++) {
    i = max(rect[a].w, i);
    j += rect[a].h;
  }
  return Rectangle(rect[2].w+rect[3].w+i, max(rect[3].h,max(rect[2].h,j)));
}

Rectangle fifth(const Rectangle rect[maxRec]) {
  int i = 0, j = 0;
  i = max(rect[0].h+rect[2].h, rect[1].h+rect[3].h);

  if (rect[1].h >= rect[2].h + rect[3].h) {
    j = max(rect[2].w, rect[3].w) + rect[1].w;
    return Rectangle(i, max(j, rect[0].w));
  }
  if (rect[1].h > rect[2].h) {
    j = max(rect[2].w, rect[3].w);
    return Rectangle(i, max(j + rect[1].w, rect[0].w + rect[3].w));
  }
  if (rect[2].h >= rect[0].h + rect[1].h) {
    j = max(rect[0].w, rect[1].w) + rect[2].w;
    return Rectangle(i, max(j, rect[3].w));
  }
  if (rect[2].h > rect[1].h) {
    j = max(rect[1].w, rect[0].w);
    return Rectangle(i, max(j + rect[2].w, rect[0].w + rect[3].w));
  }
  j = max(rect[0].w + rect[3].w, rect[1].w + rect[2].w);
  return Rectangle(i, j);
}

void minArea(const Rectangle sq) {
  if(sq.area > area) return;

  Rectangle tempRect = sq;
  tempRect.wh();
  if(tempRect.area < area) {
    area = tempRect.area;
    square.clear();
  }
  square.push_back(tempRect);
}

void pack(const Rectangle rect[maxRec]) {
  minArea(first(rect));
  minArea(second(rect));
  minArea(third(rect));
  minArea(fourth(rect));
  minArea(fifth(rect));
}

int main() {
  ofstream fout ("packrec.out");
  ifstream fin ("packrec.in");

  Rectangle rect[maxRec];
  Rectangle rectTemp[maxRec];

  int a, b;
  int permutation[4] = {0, 1, 2, 3};
  while(fin >> a >> b) {
    square.clear();
    area = 1000000;
    rect[permutation[0]] = Rectangle(a,b);

    for(int i = 1; i < maxRec; i++) {
       fin >> a >> b;
       rect[i] = Rectangle(a,b);
    }

    do {
      for(int c = 0; c < 16; c++) {
         for(int j = 0; j < maxRec; j++) {
           rectTemp[j] = rect[permutation[j]];
            if(c & (1<<j)) {
               rectTemp[j].swap();
             }
         }
         pack(rectTemp);
      }
    } while(next_permutation(permutation, permutation+4));
    square.sort();
    square.unique();
    cout << area << endl;

    while(!square.empty()) {
      int i = square.front().h;
      int j = square.front().w;
      cout << i << " " << j << endl;
      square.pop_front();
    }
  }
  return 0;
}
4

1 回答 1

4
Rectangle first(const Rectangle rect[maxRec]) {
 int i = 0, j = 0;
 for(int a = 0; a < maxRec; a++) {
     i += rect[i].w;
 //            ^------- You're adding a rectangle width to a list index
    j = max(j, rect[a].h);
 }
 return Rectangle(i,j);
}

此错误至少出现在您的代码中的其他位置。这很容易发生,因为您在代码的某些部分使用变量名称(例如i, )作为列表索引,而在另一部分使用矩形尺寸。j选择一致的约定将有助于防止此类错误。一个常见的约定是使用i,j作为索引变量。使用w,h表示宽度、高度等。

于 2013-10-15T23:51:03.487 回答