我最近註冊的USACO在線培訓計劃,我目前的問題2. 我已經完成編寫這一切都正確編譯。但是,當我提交代碼時,我不斷收到以下錯誤:運行時錯誤:非法文件打開(的/ dev/tty的)
運行1:執行錯誤:您的程序有此運行時錯誤: 非法文件打開(/ dev/tty)。該程序在錯誤發生前運行0.011 CPU 秒。它使用了3348 KB的內存。
我試圖尋找是徒勞的問題。但是,我認爲這是一個內存管理問題,因爲我使用了指針向量。在USACO常見問題解答中,他們說:
運行我們的評分系統的Linux比內存訪問更挑剔。因此,在Windows上允許的無效數組索引和錯誤的指針取消引用可能會導致您的程序在Linux下崩潰。或者,他們會破壞內部數據結構並給出最隱祕的錯誤消息,如「無法打開/ dev/[嘟嘟]」。
下面的代碼:
/*
ID: freebie1
PROG: gift1
LANG: C++
*/
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <fstream>
using namespace std;
class Person {
public:
Person():m_volatile(0) {}
void setName(string name) { m_name = name; }
string getName() { return m_name; }
void setMoney(int money){ m_sMoney = money; }
int getMoney() { return m_volatile; }
void receive(int money) { m_volatile += money; }
void giveGifts(vector<Person*> fTab){
int splitMoney = m_sMoney/fTab.size();
for(vector<Person*>::iterator it=fTab.begin();it!=fTab.end();it++) {
(*it)->receive(splitMoney);
}
m_volatile-=splitMoney*fTab.size();
}
private:
string m_name;
int m_sMoney; // starting money
int m_volatile;
};
。
class FindFriend {
public:
FindFriend(string query):m_query(query){}
bool operator()(Person &individu){
if(individu.getName()==m_query) { return true; }
else { return false; }
}
private:
string m_query;
};
vector<Person*> setFriends(vector<string> &namesTab,vector<Person> &personsTab){
vector<Person*> tab;
for(vector<string>::iterator it=namesTab.begin();it!=namesTab.end();it++) {
FindFriend f(*it);
vector<Person>::iterator trouve=find_if(personsTab.begin(),personsTab.end(),f);
tab.push_back(&*trouve);
}
return tab;
}
int main() {
ofstream fout("gift1.out");
ifstream fin("gift1.in");
if(fin) {
int np(0);
fin>>np; // Number of persons
// Each one is assigned a name...
vector<Person> personsTab(np);
for(vector<Person>::iterator it=personsTab.begin();it!=personsTab.end();it++){
string namePerson;
fin>>namePerson;
it->setName(namePerson);
}
// ...money and the friends we'll give the money to
while(!fin.eof()){
string name;
int money(0),nFriends(0);
fin>>name;
FindFriend g(name);
vector<Person>::iterator trouve=find_if(personsTab.begin(),personsTab.end(),g);
fin>>money>>nFriends;
trouve->setMoney(money); // Somme de depart
// Amis
if(nFriends!=0 || money!=0) {
vector<string> friendsTab;
for(int i(0);i<nFriends;i++) {
string chaine;
fin>>chaine;
friendsTab.push_back(chaine);
}
// We create a vector of pointers to his friends
vector<Person*> pFriends(nFriends);
pFriends=setFriends(friendsTab,personsTab);
trouve->giveGifts(pFriends); // Each person share the money among his friends
}
}
// We output the net loss/profit for each one
for(vector<Person>::iterator it=personsTab.begin();it!=personsTab.end();it++) {
string name=it->getName();
int money=it->getMoney();
fout<<name<<" "<<money<<endl;
}
}
return 0;
}
這裏是gift1.in
5
dave
laura
owen
vick
amr
dave
200 3
laura
owen
vick
owen
500 1
dave
amr
150 2
vick
owen
laura
0 2
amr
vick
vick
0 0
需要查看gift1.in的內容。我認爲最可能的錯誤不是指針向量,而是當你做find_if時,你從不檢查是否找到了你要找的東西。 – jahhaj 2012-07-29 11:16:45