六狼论坛

 找回密码
 立即注册

QQ登录

只需一步,快速开始

新浪微博账号登陆

只需一步,快速开始

搜索
查看: 43|回复: 0

USACO:Section 1.1 Greedy Gift Givers

[复制链接]

升级  72.35%

801

主题

801

主题

801

主题

探花

Rank: 6Rank: 6

积分
2447
 楼主| 发表于 2013-1-26 13:45:21 | 显示全部楼层 |阅读模式
气死我了,提交半天么成功,原来他们的“_”是指空格,气死俺了。
/*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;}
您需要登录后才可以回帖 登录 | 立即注册 新浪微博账号登陆

本版积分规则

快速回复 返回顶部 返回列表