D - NHK协会的阴谋(结构体+字符串)

题意:

求每个人所给的特征码中所能组成“NHK”的个数P;找出真正的NEET(P>K && K<L),经排序后输出。

题解

求P的个数:记录给定的字符串中“N” “H" “K”的个数,其中个数最小的即为P。这道题比较简单,实现方法有很多种。下面附上我的AC code。

                                                                                                                                                                                         

AC code

#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <vector>
#include <string>
#include <map>
#include <set>
#include <stack>
#include <queue>
#include <functional>
#include <algorithm>
#define _USE_MATH_DEFINES
using namespace std;
typedef long long LL;

struct Data
{
    char name[25];
    char str[1005];
    int p;
    int q;
    int x;
};
bool cmp(Data a,Data b)
{
    if(a.x>b.x) return true;
    else if(a.x==b.x)
    {
        if(strcmp(a.name,b.name)<0) return true;
        else return false;           //该句话不能省不然会WA
    }
    else return false;
}
Data st[105];
Data node[105];
int main()
{
    int T;
    cin>> T;
    while(T--)
    {
        memset(st,0,sizeof(st));
        memset(node,0,sizeof(node));
        int k, l, m;
        scanf("%d %d %d",&k,&l,&m);
        int t = 0;
        for(int i=1; i<=m; i++)
        {
            scanf("%s %s %d",st[i].name,st[i].str,&st[i].q);
            int a=0, b=0, c=0;
            int len = strlen(st[i].str);
            for(int j=0; j<len; j++)
            {
                if(st[i].str[j]=='N') a++;
                else if(st[i].str[j]=='H') b++;
                else if(st[i].str[j]=='K') c++;
                else continue;
            }
            if(a<=b && a<=c) st[i].p = a;
            if(b<=a && b<=c) st[i].p = b;
            if(c<=a && c<=b) st[i].p = c;
            if(st[i].p>k && st[i].q<l)
            {
                ++t;
                strcpy(node[t].name,st[i].name);
                node[t].x = st[i].p * (l - st[i].q);
            }
        }
        if(t==0) printf("FINE!\n");
        else
        {
            sort(node+1,node+1+t,cmp);
            for(int i=1; i<=t; i++)
            {
                printf("%s\n",st[i].name);
            }
        }
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/Alibaba_lhl/article/details/81228776