HDU 2093(考试排名)解题纠错
来源:互联网 发布:淘宝店女装退货率30 编辑:程序博客网 时间:2024/04/30 13:36
以下来自点击打开链接
#include <iostream>#include <iomanip>#include<algorithm>#include <string>#include <vector>using namespace std;class Problem{public: Problem(); int GetGrade(); static void SetPunish();private: int accept_time; int wrong_times; static int punish;};Problem::Problem() { int temp; cin>>temp; if(temp<0) { this->wrong_times = -temp; this->accept_time = 0; } else { if(temp==0) { this->wrong_times = 0; this->accept_time = 0; } else { this->accept_time = temp; if( cin.get()=='(') { cin>>this->wrong_times; cin.get(); } else { this->wrong_times = 0; } } } }int Problem::GetGrade(){ if( this->accept_time==0 ) { return 0; } else { return this->accept_time + this->wrong_times * this->punish; }}void Problem::SetPunish(){ cin>>Problem::punish;}class Student{public: Student(); void Calculate(); int GetGrade(); int GetAccept(); string& GetName(); void print(); static void SetPeople();private: string name; Problem *questions; int accept_problem; int grade; static int people;};Student::Student(){ cin>>this->name; this->questions = new Problem[this->people]; this->Calculate();}inline string& Student::GetName(){ return this->name;}inline int Student::GetAccept(){ return this->accept_problem;}inline int Student::GetGrade(){ return this->grade;}void Student::Calculate(){ int i, grade; this->accept_problem = 0; this->grade = 0; for(i = 0; i < this->people; i++) { grade = this->questions[i].GetGrade(); if( grade ) { this->grade += grade; this->accept_problem += 1; } }}void Student::SetPeople(){ cin>>Student::people;}bool compare(Student &a,Student &b);bool compare(Student &a,Student &b){ if( a.GetAccept() < b.GetAccept() ) { return false; } if( a.GetAccept() > b.GetAccept() ) { return true; } if( a.GetGrade() < b.GetGrade() ) { return true; } if( a.GetGrade() > b.GetGrade() ) { return false; } if( a.GetName().compare( b.GetName() ) < 0 ) { return true; } else { return false; }}void Student::print(){ cout<<setw(10)<<left<<this->GetName()<<" "<<right<<setw(2)<<this->GetAccept()<<" "<<right<<setw(4)\ <<this->GetGrade()<<endl;}int Problem::punish = 0;int Student::people = 0;int main(){ Student *p; vector<Student>students; Student::SetPeople(); Problem::SetPunish(); while( cin.peek()!=EOF ) { p = new Student(); students.push_back(*p); cin.get(); } sort(students.begin(),students.end(),compare); int i = students.size(),j; for(j = 0; j < i; j++) { students[j].print(); } return 0;}
设有以下输入:
1 20
Smith 10
John 20
John的第1个字符被该程序中cin.get()吃掉,变成了ohn。
另外,有时会多出1个数值全为0的记录。
问题出在cin.peek()!=EOF 这个条件,它不能及时地发挥作用。还是要判读入的人名是否为空。
还有,程序中的变量名people改为problem_number较为确切。
以下修改AC:
#include <iostream>#include <iomanip>#include<algorithm>#include <string>#include <vector>using namespace std;class Problem{public: Problem(); int GetGrade(); static void SetPunish();private: int accept_time; int wrong_times; static int punish;};Problem::Problem() { int temp; char c; cin>>temp; if(temp<0) { this->wrong_times = -temp; this->accept_time = 0; } else { if(temp==0) { this->wrong_times = 0; this->accept_time = 0; } else { this->accept_time = temp; if( (c=cin.get())=='(') { cin>>this->wrong_times; cin.get(); } else { this->wrong_times = 0; } } } }int Problem::GetGrade(){ if( this->accept_time==0 ) { return 0; } else { return this->accept_time + this->wrong_times * this->punish; }}void Problem::SetPunish(){ cin>>Problem::punish;}class Student{public: Student(); void Calculate(); int GetGrade(); int GetAccept(); string& GetName(); void print(); static void SetPeople();private: string name; Problem *questions; int accept_problem; int grade; static int people;};Student::Student(){ cin>>this->name; if (!this->name.empty())//加 { this->questions = new Problem[this->people]; this->Calculate(); }}inline string& Student::GetName(){ return this->name;}inline int Student::GetAccept(){ return this->accept_problem;}inline int Student::GetGrade(){ return this->grade;}void Student::Calculate(){ int i, grade; this->accept_problem = 0; this->grade = 0; for(i = 0; i < this->people; i++) { grade = this->questions[i].GetGrade(); if( grade ) { this->grade += grade; this->accept_problem += 1; } }}void Student::SetPeople(){ cin>>Student::people;}bool compare(Student &a,Student &b);bool compare(Student &a,Student &b){ if( a.GetAccept() < b.GetAccept() ) { return false; } if( a.GetAccept() > b.GetAccept() ) { return true; } if( a.GetGrade() < b.GetGrade() ) { return true; } if( a.GetGrade() > b.GetGrade() ) { return false; } if( a.GetName().compare( b.GetName() ) < 0 ) { return true; } else { return false; }}void Student::print(){ cout<<setw(10)<<left<<this->GetName()<<" "<<right<<setw(2)<<this->GetAccept()<<" "<<right<<setw(4)\ <<this->GetGrade()<<endl;}int Problem::punish = 0;int Student::people = 0;int main(){ Student *p; vector<Student>students; Student::SetPeople(); Problem::SetPunish(); while( cin.peek()!=EOF ) { p = new Student(); if (!p->GetName().empty())//加 students.push_back(*p); //删 cin.get(); } sort(students.begin(),students.end(),compare); int i = students.size(),j; for(j = 0; j < i; j++) { students[j].print(); } return 0;}
- HDU 2093(考试排名)解题纠错
- HDU 1236(排名)解题纠错
- hdu 2093 考试排名
- HDU 2093 - 考试排名
- HDU 2093 考试排名
- HDU 2093 考试排名
- hdu 2093 考试排名
- HDU 2093 考试排名
- HDU 2093 考试排名
- hdu 2093 考试排名
- hdu 2093 考试排名
- HDU 2093考试排名
- hdu---2093考试排名
- HDU考试排名2093
- 【HDU 2093】考试排名
- HDU 2093 考试排名
- hdu 2093 考试排名
- 【hdu 2093】 考试排名
- POJ 3278 Catch That Cow
- 怎样成为优秀的软件模型设计者?(转)
- 面向对象的设计法则
- 一个人,挺好的嘛
- Spring 多数据源事务配置问题
- HDU 2093(考试排名)解题纠错
- 用WebService实现对数据库进行操作(添加+删除+修改)
- JS选项卡
- getline成员函数分析
- 从Coding Fan到真正的技术专家
- joj2534
- 导入导出数据语句小结
- Java 多线程与并发编程专题
- 存储过程编写经验和优化措施