이 퍼즐을 해결하기 위해 노력하고 있습니다 : Shipping Coding Puzzle. 나는이 논리에 몇 가지 버그가있을 수 있습니다 알고g ++를 사용하는 C++ 프로그램에서 부 페이지 오류 방지
#include <fcntl.h>
#include <sys/mman.h>
#include <stdlib.h>
#include <stdio.h>
#include <math.h>
#include <sstream>
#include <set>
using namespace std;
struct Container
{
Container(int c, int w) : cost(c), weight(w){}
int cost;
int weight;
bool operator<(const Container& c) const
{
return double(cost)/weight < double(c.cost)/c.weight;
}
};
int main(int argc, char** argv)
{
int fd = open(argv[1], O_RDONLY);
char* pContent = (char*)mmap(NULL, 128 * sizeof(int), PROT_READ, MAP_PRIVATE, fd, 0);
pContent += 8;
stringstream ss(pContent);
int unload = 0;
ss >> unload;
set<Container> containers;
int cost = 0, weight = 0;
while(ss >> cost)
{
ss >> weight;
containers.insert(Container(cost, weight));
}
const Container& best = *containers.begin();
cost = best.cost * ceil(double(unload)/best.weight);
printf("%d\n", cost);
return 0;
}
: 이것은 내가 지금까지 올 한 코드입니다. 하지만 제 질문은 논리와 관련이 없습니다. 이 코드를 제출하면 성공적으로 실행되지만 점수는 마이너 페이지 결함 번호가 409
입니다. 리더 보드를 보았을 때 누군가가 사소한 페이지 결함이있는 C++ 코드를 제출했습니다 69
. 이러한 사소한 페이지 결함을 제어 할 수있는 방법이 있습니까? 일부 g ++ 플래그를 사용하고있을 수 있습니까? 지금 내 make 파일은 매우 간단합니다 : g++ -o MyExe MyExe.cc
.
이 컨텍스트에서 '부 페이지 오류'에 대해 구체적으로 설명해 주실 수 있습니까? –
나는 그것이 '수요 페이징 (demand paging)'이라고 생각한다. 그러면 다른 사람이 모두 같은 접근법을 사용하여 동일한 결과를 얻을 수있다. –
@Als 나는 그가 소프트 페이지 결함 (메모리가 표시되지 않은 경우)을 의미한다고 확신합니다. – Lockhead