1006. Team Rankings

Input

There will be multiple input sets. Input for each set is a positive integer n on a line by itself, followed by n lines (n no more than 100), each containing a permutation of the letters A, B, C, D and E, left-justified with no spaces. The final input set is followed by a line containing a 0, indicating end of input.

Output

Output for each input set should be one line of the form:

ranking is the median ranking with value value.

Of course ranking should be replaced by the correct ranking and value with the correct value. If there is more than one median ranking, you should output the one which comes first alphabetically.

Sample Input

4
ABDCE
BACDE
ABCED
ACBDE
0

Sample Output

ABCDE is the median ranking with value 4.

代码实现:

do-while 与while的不同是do-while实现进行循环体一次然后再进行条件的判断,即不管怎样,do-while循环至少会执行一次循环体

// next_permutation(arr, arr+n)
// 返回的是bool值

#include<iostream>
#include<algorithm>
#include<map>
#include<cstring>
using namespace std;

map<char, int> m;

int countvalue(string temp) {
  int sum = 0;
  for (int i = 0; i < 4; ++i)
    for (int j = i + 1; j < 5; ++j)
      if (m[temp[i]] > m[temp[j]])
        sum++;
  return sum;
}


int main() {
  int n;
  while (cin >> n && n) {
    string str = "ABCDE";
    int count = 1000000000;
    string *s = new string[n];
    for (int i = 0; i < n; ++i)
      cin >> s[i];
    string temp;
    do {
      for (int i = 0; i < 5; ++i)
        m[str[i]] = i;
      int sum = 0;
      for (int i = 0; i < n; ++i)
        sum += countvalue(s[i]);
      if (count > sum) {
        count = sum;
        temp = str;
      }
    } while (next_permutation(str.begin(), str.begin() + 5));
      cout << temp << " is the median ranking with value " << count <<"."<< endl;
  }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值