ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

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

2020-10-01 19:31:32  阅读:278  来源: 互联网

标签:1095 PAT temp Level grade scanf locations include loc


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,PAT,temp,Level,grade,scanf,locations,include,loc
来源: https://www.cnblogs.com/zchq/p/13758832.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有