|
|
气死我了,提交半天么成功,原来他们的“_”是指空格,气死俺了。
/*ID:lwj13961 TASK:gift1LANG: C++*/#include<iostream>#include<fstream>#include<vector>#include<cstring>#include<cassert>using namespace std;class person {public:string name;int out;int in; };int NP=0;//the quantities of groupsvector<person> ps;int gift(){char *ifs_file_name = "gift1.in";/* input file name */ifstreamifs;/* create ifstream object */ifs.open (ifs_file_name);/* open ifstream */if (!ifs) {cerr << "\nERROR : failed to open input file " << ifs_file_name << endl;exit (EXIT_FAILURE);}ifs>>NP;assert(NP>=2||NP<=10);//N2<=P<=10string name;person p;for(int i=0;i<NP;i++){ifs>>name;assert(name.size()<=14);// name <14 charactorsp.name = name;p.out=0;p.in=0;ps.push_back(p);}int totalmoney;int personnum;vector<person>::iterator ip;while(!ifs.eof()){ifs>>name;if(name=="") return 1;ifs>>totalmoney>>personnum;assert(totalmoney>=0||totalmoney<=2000);int getmoney;if(personnum!=0)getmoney=totalmoney/personnum;elsegetmoney=0;//find the gving person ,update his out if(!ifs.eof())for(ip=ps.begin();ip!=ps.end();ip++){if(strcmp((*ip).name.c_str(),name.c_str())==0){ (*ip).out += getmoney*personnum;//cout<<(*ip).name<<" out money:"<<(*ip).out<<endl;break;}}//find the get person,update his infor(int i=0;i<personnum;i++){string getname;ifs>>getname;for(ip=ps.begin();ip!=ps.end();ip++){if(strcmp((*ip).name.c_str(),getname.c_str())==0){(*ip).in += getmoney;//cout<<name<<" give "<<(*ip).name<<" money:"<<getmoney<<" ";//cout<<(*ip).name<<" in money:"<<(*ip).in<<endl;break;}}}}ifs.close ();/* close ifstream *///output resultchar *ofs_file_name = "gift1.out";/* output file name */ofstreamofs;/* create ofstream object */ofs.open (ofs_file_name);/* open ofstream */if (!ofs) {cerr << "\nERROR : failed to open output file " << ofs_file_name << endl;exit (EXIT_FAILURE);}vector<person>::iterator it;for(it=ps.begin();it!=ps.end();it++){ofs<<(*it).name<<" "<<(*it).in-(*it).out;ofs<<endl;}ofs.close ();/* close ofstream */}int main(){gift();return 0;} |
|