UVa12412 - A Typical Homework (a.k.a Shi Xiong Bang Bang Mang)

wrong answer很奇怪。经过几天找测试例子,终于找到原因了。在计算Overall时,计数有问题,原来用的是位操作,值>1将pass1加1,>=3将pass2加1,这种计算方式不对。

代码如下:

#include <string>
#include <vector>
#include <cstdlib>
#include <algorithm>
#include <cstdio>

using namespace std;

const double EPS = 1e-5;

struct Student
{
	string sid;
	int cid;
	string name;
	int chiScore, matScore, engScore, proScore;
};

enum Status
{
	MAIN_MENU,
	ADD,
	REMOVE,
	QUERY,
	SHOWRANK,
	SHOWSTAT
};

Status status;
vector<Student> students;

void printMainMenu();

void Add(vector<Student>& students, Student student);
void Remove(vector<Student>& students, string str);
void Query(vector<Student>& students, string str);
void ShowStat(vector<Student>& students, int cid);
int Rank(vector<Student>& students, int total);

int main()
{
#ifndef ONLINE_JUDGE
    freopen("f:\\OJ\\uva_in.txt", "r", stdin);
    freopen("f:\\OJ\\uva_out.txt", "w", stdout);
#endif

	status = MAIN_MENU;

	while (1)
	{
		if (MAIN_MENU == status)
		{
			printMainMenu();
			int opera;
			scanf("%d", &opera);

			if (0 == opera) break;
			if (1 == opera) status = ADD;
			else if (2 == opera) status = REMOVE;
			else if (3 == opera) status = QUERY;
			else if (4 == opera) status = SHOWRANK;
			else if (5 == opera) status = SHOWSTAT;
		}
		else if (ADD == status)
		{
			printf("Please enter the SID, CID, name and four scores. Enter 0 to finish.\n");
			Student stu;
			char tmp[20];
			scanf("%s", tmp);
			stu.sid = tmp;
			if ("0" == stu.sid)
			{
				status = MAIN_MENU;
				continue;
			}

			scanf("%d%s%d%d%d%d", &stu.cid, tmp, &stu.chiScore, &stu.matScore, &stu.engScore, &stu.proScore);
			stu.name = tmp;

			Add(students, stu);
		}
		else if (REMOVE == status)
		{
			printf("Please enter SID or name. Enter 0 to finish.\n");
			string str;
			char tmp[20];
			scanf("%s", tmp);
			str = tmp;
			if ("0" == str)
			{
				status = MAIN_MENU;
				continue;
			}
			Remove(students, str);
		}
		else if (QUERY == status)
		{
			printf("Please enter SID or name. Enter 0 to finish.\n");
			string str;
			char tmp[20];
			scanf("%s", tmp);
			str = tmp;
			if ("0" == str)
			{
				status = MAIN_MENU;
				continue;
			}
			Query(students, str);
		}
		else if (SHOWRANK == status)
		{
			printf("Showing the ranklist hurts students' self-esteem. Don't do that.\n");
			status = MAIN_MENU;
		}
		else if (SHOWSTAT == status)
		{
			printf("Please enter class ID, 0 for the whole statistics.\n");
			string str;
			char tmp[20];
			scanf("%s", tmp);
			str = tmp;

			status = MAIN_MENU;

			ShowStat(students, atoi(str.c_str()));
		}
	}

	return 0;
}

void printMainMenu()
{
	printf("Welcome to Student Performance Management System (SPMS).\n\n");
	printf("1 - Add\n");
	printf("2 - Remove\n");
	printf("3 - Query\n");
	printf("4 - Show ranking\n");
	printf("5 - Show Statistics\n");
	printf("0 - Exit\n");
	printf("\n");
}

void Add(vector<Student>& students, Student student)
{
	for (size_t i = 0; i < students.size(); i++)
	{
		if (students[i].sid == student.sid)
		{
			printf("Duplicated SID.\n");
			return;
		}
	}

	students.push_back(student);
}

void Remove(vector<Student>& students, string str)
{
	int num = 0;

	for (vector<Student>::iterator it = students.begin(); it != students.end(); it++)
	{
		if (it->sid == str || it->name == str)
		{
			students.erase(it);
			num++;
			it--;
		}
	}



	printf("%d student(s) removed.\n", num);
}

void Query(vector<Student>& students, string str)
{
	for (size_t i = 0; i < students.size(); i++)
	{
		if (students[i].sid == str || students[i].name == str)
		{
			int total = students[i].chiScore + students[i].matScore + students[i].engScore + students[i].proScore;
			double aver = total / 4.0;

            printf("%d %s %d %s %d %d %d %d %d %.2f\n", Rank(students, total), students[i].sid.c_str(), students[i].cid, students[i].name.c_str(), students[i].chiScore, students[i].matScore, students[i].engScore, students[i].proScore, total, aver + EPS);
		}
	}
}

void ShowStat(vector<Student>& students, int cid)
{
	vector<Student> v;

	if (0 == cid) v.assign(students.begin(), students.end());
	else
	{
		for (size_t i = 0; i < students.size(); i++)
		{
			if (students[i].cid == cid)
			{
				v.push_back(students[i]);
			}
		}
	}
	int chiTotal = 0, matTotal = 0, engTotal = 0, proTotal = 0;
	int pass4 = 0, pass3 = 0, pass2 = 0, pass1 = 0, pass0 = 0;
	int passChi = 0, passMat = 0, passEng = 0, passPro = 0;

	for (size_t i = 0; i < v.size(); i++)
	{
		int tmp = 0;
		if (v[i].chiScore >= 60)
		{
			passChi++;
			tmp++;
		}
		chiTotal += v[i].chiScore;

		if (v[i].matScore >= 60)
		{
			passMat++;
			tmp++;
		}
		matTotal += v[i].matScore;

		if (v[i].engScore >= 60)
		{
			passEng++;
			tmp++;
		}
		engTotal += v[i].engScore;

		if (v[i].proScore >= 60)
		{
			passPro++;
			tmp++;
		}
		proTotal += v[i].proScore;

		if (tmp == 0) pass0++;
		if (tmp >= 1) pass1++;
		if (tmp >= 2) pass2++;
		if (tmp >= 3) pass3++;
		if (tmp >= 4) pass4++;
	}

	printf("Chinese\n");
	printf("Average Score: %.2f\n", (double)chiTotal / v.size() + EPS);
	printf("Number of passed students: %d\n", passChi);
	printf("Number of failed students: %d\n", v.size() - passChi);
	printf("\n");

	printf("Mathematics\n");
	printf("Average Score: %.2f\n", (double)matTotal / v.size() + EPS);
	printf("Number of passed students: %d\n", passMat);
	printf("Number of failed students: %d\n", v.size() - passMat);
	printf("\n");

    printf("English\n");
	printf("Average Score: %.2f\n", (double)engTotal / v.size() + EPS);
	printf("Number of passed students: %d\n", passEng);
	printf("Number of failed students: %d\n", v.size() - passEng);
	printf("\n");

    printf("Programming\n");
	printf("Average Score: %.2f\n", (double)proTotal / v.size() + EPS);
	printf("Number of passed students: %d\n", passPro);
	printf("Number of failed students: %d\n", v.size() - passPro);
	printf("\n");

	printf("Overall:\n");
    printf("Number of students who passed all subjects: %d\n", pass4);
	printf("Number of students who passed 3 or more subjects: %d\n", pass3);
	printf("Number of students who passed 2 or more subjects: %d\n", pass2);
	printf("Number of students who passed 1 or more subjects: %d\n", pass1);
	printf("Number of students who failed all subjects: %d\n", pass0);
	printf("\n");
}

int Rank(vector<Student>& students, int total)
{
	int ans = 0;

	for (size_t i = 0; i < students.size(); i++)
	{
		int tmp = students[i].chiScore + students[i].matScore + students[i].engScore + students[i].proScore;
		if (tmp > total) ans++;
	}

	return ans + 1;
}




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

kgduu

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值