算法提高 判断名次
时间限制:1.0s 内存限制:256.0MB
问题描述
某场比赛过后,你想要知道A~E五个人的排名是什么,于是要求他们每个人说了一句话。(经典的开头……-_-!)得了第1名的人23,说了假话;得了第5名的人不好意思,也说了假话;为了使求解问题简单,第3名同样说了假话。(奇数名次说假话)
输入格式
共5行,各行依次表示A~E说的话。
每行包含一个形如“A>=3”的名次判断,即一个大写字母+关系运算符+一个数字,不包含空格。
大写字母A~E,关系运算<、<=、=、>=、>、!=,数字1~5。注意:等于是“=”不是“==”!
输出格式
可能有多解,请按照字典序输出排名序列,每个解一行
最后一行输出解的数量
样例输入
A=2
D=5
E>3
A>2
B!=1
样例输出
ACDEB
AECBD
BADCE
BCADE
BDACE
CEADB
CEBDA
7
#include <iostream>
#include <algorithm>
using namespace std;
char people[5];
string rel[5];
int say_rank[5];
char solutions[6] = {'A', 'B', 'C', 'D', 'E'};
int sol_rank[5];
int num_sol = 0;
int is_true(int idx)
{
if (rel[idx] == "<")
{
if (sol_rank[people[idx] - 'A'] < say_rank[idx])
return 1;
else
return 0;
}
else if (rel[idx] == "<=")
{
if (sol_rank[people[idx] - 'A'] <= say_rank[idx])
return 1;
else
return 0;
}
else if (rel[idx] == "=")
{
if (sol_rank[people[idx] - 'A'] == say_rank[idx])
return 1;
else
return 0;
}
else if (rel[idx] == ">=")
{
if (sol_rank[people[idx] - 'A'] >= say_rank[idx])
return 1;
else
return 0;
}
else if (rel[idx] == ">")
{
if (sol_rank[people[idx] - 'A'] > say_rank[idx])
return 1;
else
return 0;
}
else
{
if (sol_rank[people[idx] - 'A'] != say_rank[idx])
return 1;
else
return 0;
}
}
int main()
{
string input;
for (int i = 0; i < 5; ++i)
{
cin >> input;
people[i] = input[0];
rel[i] = input.substr(1, input.length() - 2);
say_rank[i] = input[input.length() - 1] - '0';
}
do {
for (int i = 0; i < 5; ++i)
sol_rank[solutions[i] - 'A'] = i + 1;
int flag = 1;
for (int i = 0; i < 5; ++i)
{
if ( (i % 2 == 0 && is_true(solutions[i] - 'A')) || (i % 2 == 1 && (!is_true(solutions[i] - 'A'))) )
{
flag = 0;
break;
}
}
if (flag == 1)
{
cout << solutions << endl;
num_sol++;
}
} while (next_permutation(solutions, solutions + 5));
cout << num_sol;
return 0;
}