PAT Advanced Level (1081~1087)

PAT 1081 Rational Sum

链接:http://www.patest.cn/contests/pat-a-practise/1081

代码:注意有总和为零的情况,此时要输出“0”

#include<iostream>
#include<string>
#include<cstdio>
#include<cstring>
#include<set>
#include<cmath>
#include<algorithm>
using namespace std;
long long gcd(long long x,long long b)
{
    long long c;
    for(;b!=0;)
    {
        c=x%b;
        x=b;
        b=c;
    }
    return x;
}
int main()
{
    int n;
    long long a,b,c,d,e;
    scanf("%lld",&n);
    scanf("%lld/%lld",&a,&b);
    for(int i=1;i<n;++i)
    {
        scanf("%lld/%lld",&c,&d);
        e=gcd(b,d);
        e=b/e*d;
        a=a*(e/b);
        c=c*(e/d);
        a=a+c;
        b=e;
    }
    long long f=a/b;
    a=a%b;
    if(f!=0||a==0)
    {
        printf("%lld",f);
    }
    if(a!=0)
    {
        c=gcd(a,b);
        if(f==0)
            printf("%lld/%lld",a/c,b/c);
        else
            printf(" %lld/%lld",a/c,b/c);
    }
    printf("\n");
    return 0;
}

PAT 1082  Read Number in Chinese

链接:http://www.patest.cn/contests/pat-a-practise/1082

代码:

#include<iostream>
#include<string>
#include<cstdio>
#include<cstring>
#include<vector>
#include<set>
#include<cmath>
#include<algorithm>
using namespace std;
string d[11]= {"ling", "yi", "er", "san", "si", "wu", "liu", "qi", "ba", "jiu"};
string q[11]= {"", "Shi", "Bai", "Qian", "Wan", "Shi", "Bai", "Qian", "Yi"};
vector<int> res;
vector<string> ans;
int main()
{
    int n;
    scanf("%d",&n);
    if(n==0)
    {
        printf("ling\n");
    }
    else
    {
        int len=0;
        if(n<0)
        {
            printf("Fu ");
            n=-n;
        }
        for(; n>0; n/=10)
        {
            res.push_back(n%10);
        }
        for(; len<res.size()&&res[len]==0; ++len);
        if(len<8)
        {
            for(int i=len; i<res.size(); ++i)
            {
                if(i!=0&&(res[i]!=0||i%4==0))
                {
                    ans.push_back(q[i]);
                }
                ans.push_back(d[res[i]]);
            }
            for(int i=ans.size()-1,j; i>=0; --i)
            {
                j=0;
                if(i!=ans.size()-1)
                    printf(" ");
                for(; i>=0&&ans[i]=="ling"; --i,++j);
                if(j>0&&ans[i]!="Wan")
                    printf("ling ");
                printf("%s",ans[i].c_str());
            }
            printf("\n");
        }
        else
        {
            printf("%s Yi\n",d[res[len]].c_str());
        }
    }
    return 0;
}

PAT 1083  List Grades

链接:http://www.patest.cn/contests/pat-a-practise/1083

代码:

#include<iostream>
#include<string>
#include<cstdio>
#include<cstring>
#include<vector>
#include<set>
#include<cmath>
#include<algorithm>
using namespace std;
struct student
{
    char name[15];
    char id[15];
    int grade;
} stu[100005];
bool cmp(const struct student &a,const struct student &b)
{
    return a.grade<b.grade;
}
int main()
{
    int n,a,b;
    int maxGrade=-1,minGrade=999;
    scanf("%d",&n);
    for(int i=0; i<n; ++i)
    {
        scanf("%s %s %d",stu[i].name,stu[i].id,&stu[i].grade);
        if(stu[i].grade>maxGrade) maxGrade=stu[i].grade;
        if(stu[i].grade<minGrade) minGrade=stu[i].grade;
    }
    sort(stu,stu+n,cmp);
    scanf("%d%d",&a,&b);
    if(a>b)
    {
        a=a^b;
        b=a^b;
        a=a^b;
    }
    if(b<minGrade||a>maxGrade)
    {
        printf("NONE\n");
    }
    else
    {
        int idxA=0;
        int idxB=n-1;
        for(;idxA<n&&stu[idxA].grade<a;++idxA);
        for(;idxB>=0&&stu[idxB].grade>b;--idxB);
        //printf("%d\n",idxB-idxA+1);
        for(int i=idxB;i>=idxA;--i)
            printf("%s %s\n",stu[i].name,stu[i].id);
    }
    return 0;
}

PAT 1086  Tree Traversals Again

链接:http://www.patest.cn/contests/pat-a-practise/1086

代码:

#include<iostream>
#include<string>
#include<cstdio>
#include<cstring>
#include<vector>
#include<set>
#include<stack>
#include<cmath>
#include<algorithm>
using namespace std;
struct point
{
    int sons[2];
} node[35];
bool first=true;
int n,m,root;
void postorder(int idx)
{
    for(int i=0; i<2; ++i)
        if(node[idx].sons[i]!=-1)
            postorder(node[idx].sons[i]);
    if(first)
    {
        printf("%d",idx);
        first=false;
    }
    else
        printf(" %d",idx);
}
int inorder()
{
    int id=-1;
    char s[10];
    if(m<2*n)
    {
        scanf("%s",s);
        if(strcmp(s,"Push")==0)
        {
            scanf("%d",&id);
            if(m==0) root=id;
            ++m;
        }
        else
        {
            ++m;
            return -1;
        }
        node[id].sons[0]=inorder();
        node[id].sons[1]=inorder();
    }
    return id;
}
int main()
{
    m=0;
    scanf("%d",&n);
    for(int i=1; i<=n; ++i)
    {
        node[i].sons[0]=node[i].sons[1]=-1;
    }
    inorder();
    postorder(root);
    return 0;
}

PAT 1087 All Roads Lead to Rome

链接:http://www.patest.cn/contests/pat-a-practise/1087

代码:

#include<iostream>
#include<string>
#include<cstdio>
#include<cstring>
#include<vector>
#include<set>
#include<map>
#include<cmath>
#include<algorithm>
using namespace std;
struct road
{
    vector<int> path;
    int costSum;
    int happinessSum;
}tmp;
map<int,string> idToName;
map<string,int> nameToId;
map<int,int> idToHappiness;
vector<int> path;
vector<struct road> ans;
bool visited[205];
int mapx[205][205];
int n;
void dfs(int st,int ed,int costSum,int happiniessSum)
{
    if(st==ed)
    {
        tmp.path.clear();
        int len=path.size();
        for(int i=0;i<len;++i)
            tmp.path.push_back(path[i]);
        tmp.costSum=costSum;
        tmp.happinessSum=happiniessSum;
        ans.push_back(tmp);
        return;
    }
    for(int i=0;i<n;++i)
    {
        if(mapx[st][i]>0&&(!visited[i]))
        {
            path.push_back(i);
            costSum+=mapx[st][i];
            happiniessSum+=idToHappiness[i];
            visited[i]=true;
            dfs(i,ed,costSum,happiniessSum);
            visited[i]=false;
            costSum-=mapx[st][i];
            happiniessSum-=idToHappiness[i];
            path.pop_back();
        }
    }
}
bool cmp(const struct road &a,const struct road &b)
{
    return a.costSum!=b.costSum?(a.costSum<b.costSum):(a.happinessSum!=b.happinessSum?(a.happinessSum>b.happinessSum):(a.happinessSum/(double)(a.path.size()-1)>b.happinessSum/(double)(b.path.size()-1)));
}
int main()
{
    int k,a,endCity;
    char c[15],s[15];
    scanf("%d%d%s",&n,&k,c);
    idToName.insert(make_pair(0,string(c)));
    nameToId.insert(make_pair(string(c),0));
    idToHappiness.insert(make_pair(0,0));
    for(int i=1; i<n; ++i)
    {
        scanf("%s%d",c,&a);
        idToName.insert(make_pair(i,string(c)));
        nameToId.insert(make_pair(string(c),i));
        idToHappiness.insert(make_pair(i,a));
        if(strcmp(c,"ROM")==0) endCity=i;
    }
    memset(mapx,0,sizeof(mapx));
    for(int i=0,ida,idb;i<k;++i)
    {
        scanf("%s%s%d",c,s,&a);
        ida=nameToId[string(c)];
        idb=nameToId[string(s)];
        mapx[ida][idb]=mapx[idb][ida]=a;
    }
    memset(visited,false,sizeof(visited));
    path.push_back(0);
    visited[0]=true;
    dfs(0,endCity,0,0);
    sort(ans.begin(),ans.end(),cmp);
    int out=ans[0].costSum,countx=1;
    for(;out==ans[countx].costSum;++countx);
    printf("%d %d %d %d\n",countx,ans[0].costSum,ans[0].happinessSum,(int)(ans[0].happinessSum/(double)(ans[0].path.size()-1)));
    printf("%s",idToName[ans[0].path[0]].c_str());
    for(int i=1;i<ans[0].path.size();++i)
         printf("->%s",idToName[ans[0].path[i]].c_str());
    printf("\n");
    return 0;
}




你可能感兴趣的:(PAT Advanced Level (1081~1087))