码迷,mamicode.com
首页 > 编程语言 > 详细

HD-ACM算法专攻系列(17)——考试排名

时间:2017-06-27 21:17:36      阅读:148      评论:0      收藏:0      [点我收藏+]

标签:log   int start   algo   cout   技术分享   tar   pac   输出   stream   

问题描述:

技术分享

 

 技术分享

 

源码:

主要要注意输出格式.

 

#include"iostream"
#include"iomanip"
#include"algorithm"
#include"string"
using namespace std;

struct Person
{
	string name;
	int	count;
	int score;
};

bool cmp(Person a, Person b)
{
	if(a.count > b.count)
	{
		return true;
	}
	else if(a.count == b.count)
	{
		if(a.score < b.score)
		{
			return true;
		}
		else if(a.score == b.score)
		{
			return a.name < b.name;
		}
		else
		{
			return false;
		}
	}
	else
	{
		return false;
	}
}

int atoi(string str, int start, int end)
{
	int result = 0;
	for(int i = start; i <= end; i++)
	{
		result = result * 10 + (str[i] - ‘0‘);
	}
	return result;
}

int main()
{
	int n, m, index = 0;
	string str;
	Person *p = new Person[1000];
	cin>>n>>m;
	while(cin>>p[index].name)
	{
		p[index].count = 0;
		p[index].score = 0;
		for(int j = 0; j < n; j++)
		{
			cin>>str;
			if(str[0] != ‘-‘ && str[0] != ‘0‘)
			{
				p[index].count++;
				if(str[str.length() - 1] == ‘)‘)
				{
					for(int k = 0; k < str.length(); k++)
					{
						if(str[k] == ‘(‘)
						{
							p[index].score += atoi(str, 0, k - 1);
							p[index].score += atoi(str, k+1, str.length() - 2) * m;
							break;
						}
					}
				}
				else
				{
					p[index].score += atoi(str, 0, str.length() - 1);
				}
			}
		}
		index++;
		//if(index == 6)break;
	}
	sort(p, p + index, cmp);
	for(int i = 0; i < index; i++)
	{
		
		cout<<std::left<<setw(10)<<p[i].name<<" "<<std::right<<setw(2)<<p[i].count<<" "<<setw(4)<<p[i].score<<endl;
	}
	
    return 0;
}

  

HD-ACM算法专攻系列(17)——考试排名

标签:log   int start   algo   cout   技术分享   tar   pac   输出   stream   

原文地址:http://www.cnblogs.com/OneForCheng/p/7087146.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!