Online JudgeProblem SetAuthorsOnline ContestsUser
Web Board
Home Page
F.A.Qs
Statistical Charts
Problems
Submit Problem
Online Status
Prob.ID:
Register
Update your info
Authors ranklist
Current Contest
Past Contests
Scheduled Contests
Award Contest
User ID:
Password:
  Register

C++参考

Posted by freelark at 2016-11-30 11:36:27 on Problem 1013
#include<iostream>
#include<stdlib.h>
#include <string>
#include <map>
#include <vector>
#include <algorithm>
using namespace std;

typedef pair<char, int> PAIR;
bool cmp_by_value(const PAIR& x, const PAIR& y) {
	return abs(x.second) > abs(y.second);
}

int main(void)
{
	int n;
	string s[3];
	map<char, int> coin;
	cin >> n;
	while (n-- > 0)
	{
		coin.clear();
		for (int m = 0; m < 3; m++)
		{
			for (int i = 0; i < 3; i++)
				cin >> s[i];
			if (s[2] == "even")
			{
				for (int i = 0; i < s[0].length(); i++)
				{
					coin[s[0][i]] = 5;
					coin[s[1][i]] = 5;
				}
			}
			else if (s[2] == "up")
			{
				for (int i = 0; i < s[0].length(); i++)
				{
					if (coin[s[0][i]] != 5) coin[s[0][i]] += 1;
					if (coin[s[1][i]] != 5) coin[s[1][i]] -= 1;
				}
			}
			else if (s[2] == "down")
			{
				for (int i = 0; i < s[0].length(); i++)
				{
					if (coin[s[0][i]] != 5)  coin[s[0][i]] -= 1;
					if (coin[s[1][i]] != 5)  coin[s[1][i]] += 1;
				}
			}
		}
		vector<PAIR> coin_vec(coin.begin(), coin.end());
		for (int i = 0; i != coin_vec.size(); ++i)
		if (coin_vec[i].second == 5)
			coin_vec[i].second = 0;
		sort(coin_vec.begin(), coin_vec.end(), cmp_by_value);
		if (coin_vec.begin()->second > 0)
			cout << coin_vec.begin()->first << " is the counterfeit coin and it is heavy." << endl;
		else
			cout << coin_vec.begin()->first << " is the counterfeit coin and it is light." << endl;
	}
	system("pause");
	return 0;
}

Followed by:

Post your reply here:
User ID:
Password:
Title:

Content:

Home Page   Go Back  To top


All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator