一道acm题,我快被折腾疯了,求助

太上绝情 2011-08-16 11:03:32
题目:http://acm.hdu.edu.cn/showproblem.php?pid=2093
我的代码

#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;
}



告诉我错哪了,或者在我的基础上改对,再不济也别给c语言的代码和用<stdio.h>的c++代码
我快被折腾疯了,多谢各位了
...全文
167 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
太上绝情 2011-08-17
  • 打赏
  • 举报
回复
多谢logiciel兄弟费心帮我调试改正,同时也多谢gjlzjb兄弟的辛苦,我写代码用了一个小时,为了输入的eof的问题折腾了一天,c++的eof的问题到现在没搞清楚,一直想写c++之父所说的优雅的表示方式,结果就一直卡住了。
gjlzjb 2011-08-17
  • 打赏
  • 举报
回复
我在帮你调试,可能是你的输入有问题,可以参考下以下代码的输入,用你的代码A过后,我第一时间发给你。

#include<iostream>
#include <stdio.h>
#include<cstring>
using namespace std;
struct student
{
char name[10];
int sum;
int temp;
int solved[13];
};
int main()
{
int n,m;
cin>>n>>m;
char b[10];
student a[1000];
int k=0;
while(cin>>b)
{
strcpy(a[k].name,b);
a[k].sum=0;
a[k].temp=0;
for(int i=0;i<n;i++)
{
cin>>a[k].solved[i];
if(a[k].solved[i]>0)
{
a[k].temp+=a[k].solved[i];
a[k].sum++;
}
if(getchar()=='(')
{
int x;
cin>>x;
a[k].temp+=x*m;
getchar();
}
}
k++;
}
for(int i=0;i<k;i++)
{
for(int j=0;j<k-i-1;j++)
if(a[j].sum<a[j+1].sum)
{
student b;
b=a[j],a[j]=a[j+1],a[j+1]=b;
if(a[j].sum==a[j+1].sum)
{
if(a[j].temp>a[j+1].temp)
b=a[j],a[j]=a[j+1],a[j+1]=b;
else if(a[j].temp==a[j+1].temp)
{
if(a[j].name>a[j+1].name)
b=a[j],a[j]=a[j+1],a[j+1]=b;
}
}
}
printf("%-10s %2d %4d\n",a[i].name,a[i].sum,a[i].temp);
}
}
logiciel 2011-08-17
  • 打赏
  • 举报
回复
另外,建议把变量名people改为problem_number较为确切。
logiciel 2011-08-17
  • 打赏
  • 举报
回复
5楼不准确,问题不在那段代码。以下修改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;
}

logiciel 2011-08-17
  • 打赏
  • 举报
回复
以下有问题:
if( cin.get()=='(')
{
cin>>this->wrong_times;
cin.get();
}
else
{
this->wrong_times = 0;
}

请试以下输入:

1 20
Smith 10
John 20

John的第1个字符被cin.get(),变成了ohn。
Ulfsaar 2011-08-17
  • 打赏
  • 举报
回复
告诉我错哪了,或者在我的基础上改对,再不济也别给c语言的代码和用<stdio.h>的c++代码
我快被折腾疯了,多谢各位了

gjlzjb 兄,人家不要C代码的,你还屁颠屁颠的给人贴代码,何必呢
mapeisarah 2011-08-17
  • 打赏
  • 举报
回复
多学习学习
gjlzjb 2011-08-17
  • 打赏
  • 举报
回复
lz你用了好多面向对象的手法,我不是太理解,我仿照你的方法写了一个简单的,就AC了,你的算法是没有问题的,可能是输入输出的问题,祝好。

#include<iostream>
#include <stdio.h>
#include<cstring>
#include <algorithm>
#include <vector>
using namespace std;
struct student
{
char name[10];
int sum;
int temp;
};

bool com(student a, student b)
{
if(a.sum > b.sum) return true;
if(a.sum < b.sum) return false;

if(a.temp < b.temp) return true;
if(a.temp > b.temp) return false;

if(a.name>b.name) return true;
else return false;
}
int main()
{
int n,m;
cin>>n>>m;
char b[10];
vector<student> a;
int solved;
student p;
while(cin>>p.name)
{
p.sum=0;
p.temp=0;
for(int i=0;i<n;i++)
{
cin>>solved;
if(solved>0)
{
p.temp+=solved;
p.sum++;
}
if(cin.get()=='(')
{
int x;
cin>>x;
p.temp+=x*m;
cin.get();
}
}
a.push_back(p);
}
sort(a.begin(),a.end(),com);
for(int i=0;i<a.size();++i)
printf("%-10s %2d %4d\n",a[i].name,a[i].sum,a[i].temp);
}


太上绝情 2011-08-17
  • 打赏
  • 举报
回复
最后通过了


#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
{

cin.unget();
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(string name);
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(string name)
{
this->name = 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;
string name;
vector<Student>students;
Student::SetPeople();
Problem::SetPunish();
while( cin>>name )//让输入流自己去搞定吧,我不管他了
{
p = new Student(name);
students.push_back(*p);

}
sort(students.begin(),students.end(),compare);
int i = students.size(),j;
for(j = 0; j < i; j++)
{
students[j].print();
}


return 0;
}


64,701

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧