PAT(Basic Level) Practice : 1095 解码PAT准考证 (25分)

1095 解码PAT准考证 (25分)

参考:> https://blog.csdn.net/shiliang97/article/details/100592165

测试点3,4段错误

开辟的大数组每次都要更新,全部归零。

测试点2:考场编号输入008,输出不能是8,必须也是008

代码(测试点3超时)

#include <iostream>
#include <vector>
#include <string>
#include <cstdio>
//scanf printf防止超时
#include <algorithm>
//vector的sort
#include <sstream>
//转换
using namespace std;

#include<iomanip>
//精度

#include<cmath>
//round四舍五入取整
#include <map>

class student
{
public:
    string name;
    int grade;
};

bool compare1(student stu1,student stu2)
{
    if(stu1.grade!=stu2.grade)
        return stu1.grade>stu2.grade;
    else
        return stu1.name.compare(stu2.name)<0;
}


class location
{
public:
    int name;
    string str;
    int count;
};

bool compare2(location loc1,location loc2)
{
    if(loc1.count!=loc2.count)
        return loc1.count>loc2.count;
    else
        return loc1.name<loc2.name;
}
int Hash[1000]={0};
int main()
{
    int n,m;
    //cin>>n>>m;
    scanf("%d %d",&n,&m);
    vector<student> students;
    for(int i=0;i<n;i++)
    {
        string str;
        char str1[14];
        int grade;
        //cin>>str>>grade;
        scanf("%s %d",str1,&grade);
        str=str1;
        student temp;
        temp.name=str;
        temp.grade=grade;
        students.push_back(temp);
    }
    for(int i=0;i<m;i++)
    {
        int type;
        //cin>>type;
        scanf("%d",&type);
        if(type==1)
        {
            char c;
            cin>>c;
            //scanf("%c",&c);
            //cout<<"Case "<<i+1<<": "<<type<<" "<<c<<endl;
            printf("Case %d: %d %c\n",i+1,type,c);
            vector<student> stus;

            for(int j=0;j<students.size();j++)
            {
                if(students[j].name[0]==c)
                {
                    stus.push_back(students[j]);
                }
            }
            sort(stus.begin(),stus.end(),compare1);
            for(int j=0;j<stus.size();j++)
            {
                //cout<<stus[j].name<<" "<<stus[j].grade<<endl;
                printf("%s %d\n",stus[j].name.c_str(),stus[j].grade);
            }
            if(stus.size()==0)
            {
                //cout<<"NA"<<endl;
                printf("NA\n");
            }
        }else if(type==2)
        {
            string loc;
            char loc1[4];
            //cin>>loc;
            scanf("%s",loc1);
            loc=loc1;
            //cout<<"Case "<<i+1<<": "<<type<<" "<<loc<<endl;
            printf("Case %d: %d %s\n",i+1,type,loc1);

            int count=0;
            int sum=0;

            for(int j=0;j<students.size();j++)
            {
                string str=students[j].name.substr(1,3);

                if(str==loc)
                {
                    count++;
                    sum+=students[j].grade;
                }
            }
            if(count>0)
            {
                //cout<<count<<" "<<sum<<endl;
                printf("%d %d\n",count,sum);
            }
            else
            {
                //cout<<"NA"<<endl;
                printf("NA\n");
            }


        }else
        {
            string date;
            char date1[7];

            //cin>>date;
            scanf("%s",date1);
            date=date1;
            //cout<<"Case "<<i+1<<": "<<type<<" "<<date<<endl;
            printf("Case %d: %d %s\n",i+1,type,date1);
            vector<student> stus;
            for(int j=0;j<students.size();j++)
            {
                string str=students[j].name.substr(4,6);

                if(str==date)
                {
                    stus.push_back(students[j]);
                }
            }

            vector<location> locations;
            for(int j=0;j<stus.size();j++)
            {
                int temp;
                string str=stus[j].name.substr(1,3);
                stringstream ss;
                ss<<str;
                ss>>temp;

                if(Hash[temp]==0)
                {
                    location loc;
                    loc.name=temp;
                    loc.str=str;
                    loc.count=1;
                    locations.push_back(loc);
                    Hash[temp]=locations.size();

                }else
                {
                    locations[Hash[temp]-1].count++;
                }
            }
            sort(locations.begin(),locations.end(),compare2);
            if(locations.size()==0)
            {
                //cout<<"NA"<<endl;
                printf("NA\n");
            }
            else
            {
                for(int j=0;j<locations.size();j++)
                {
                    //cout<<locations[j].str<<" "<<locations[j].count<<endl;
                    printf("%s %d\n",locations[j].str.c_str(),locations[j].count);
                }
            }

            for(int j=0;j<locations.size();j++)
            {
                Hash[locations[j].name]=0;
            }
        }

    }
    return 0;
}

上一篇:1095


下一篇:Better late than never~ Oxygen Forensic Detective supports APK Downgrade Extraction now~