HDU 3293 sort [Ad Hoc]

#Description
三个关键字 都是字符串,排序
#Algorithm
用C++ 的 sort函数 和 字符串类
#Code

#include <cstdio>
#include <iostream>
#include <algorithm>
using namespace std;
const int maxn = 500 + 9;
int t = 0;
int n;
struct V
{
  string name, origin, level;
  int l;
};
bool comp(const V &a, const V &b)
{
  if (a.origin < b.origin ||
      (a.origin == b.origin && a.l < b.l) ||
      (a.origin == b.origin && a.level == b.level && a.name < b.name))
     return true;
  return false;
}
void solve()
{
  t++;
  cout << "Case " << t << endl;
  V a[maxn];
  for (int i = 0; i < n; i++)
  {
    cin >> a[i].name >> a[i].origin >> a[i].level;
    if (a[i].level == "wonderful") a[i].l = 0; else
      if (a[i].level == "good") a[i].l = 1; else
        if (a[i].level == "so-so") a[i].l = 2;
  }
  sort(a, a + n, comp);
  cout << a[0].origin << ":\n";
  cout << "          " << a[0].name << ' ' << a[0].level << endl;
  for (int i = 1; i < n; i++)
  {
    if (a[i].origin != a[i - 1].origin)
      cout << a[i].origin << ":\n";
    cout << "          " << a[i].name << ' ' << a[i].level << endl;
  }
}
int main()
{
  //freopen("output.txt", "w", stdout);
  while (cin >> n) solve();
}

猜你喜欢

转载自blog.csdn.net/YYecust/article/details/50929597