PAT Advanced Level (1077~1080)

PAT 1077 Kuchiguse

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

代码:

#include<iostream>
#include<string>
#include<cstdio>
#include<cstring>
#include<set>
#include<algorithm>
using namespace std;
int main()
{
    int n;
    char s[300];
    string basic,nex;
    scanf("%d",&n);
    getchar();
    gets(s);
    basic=string(s);
    reverse(basic.begin(),basic.end());
    for(int i=1;i<n;++i)
    {
        int idx=0;
        gets(s);
        nex=string(s);
        reverse(nex.begin(),nex.end());
        for(;idx<nex.length()&&idx<basic.length();++idx)
            if(nex[idx]!=basic[idx])
                  break;
        basic=basic.substr(0,idx);
    }
    if(basic.length()>0)
    {
        reverse(basic.begin(),basic.end());
        printf("%s\n",basic.c_str());
    }
    else
    {
        printf("nai\n");
    }
    return 0;
}



PAT 1078 Hashing

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

代码:

#include<iostream>
#include<string>
#include<cstdio>
#include<cstring>
#include<set>
#include<cmath>
#include<algorithm>
using namespace std;
int h[10105];
bool flag[10105];
void calPrime()
{
    memset(flag,false,sizeof(flag));
    flag[0]=flag[1]=true;
    for(int i=3; i<sqrt(10105)+1; i+=2)
    {
        if(flag[i]==false)
        {
            for(int j=i+i; j<10105; j+=i)
                flag[j]=true;
        }
    }
}
int main()
{
    int n,m,a;
    memset(h,-1,sizeof(h));
    calPrime();
    scanf("%d%d",&m,&n);
    if(m<=2)
        m=2;
    else
    {
        if(m%2==0) ++m;
        for(; m<10055; m+=2)
            if(flag[m]==false)
                break;
    }
    //printf("%d\n",m);
    for(int k=0; k<n; ++k)
    {
        scanf("%d",&a);
        int idx=a%m;
        if(h[idx]==-1)
        {
            if(k==0)
                printf("%d",idx);
            else
                printf(" %d",idx);
            h[idx]=a;
        }
        else
        {
            bool flag=false;
            for(int i=1; i<m; ++i)
            {
                idx=(a+i*i)%m;
                if(h[idx]==-1)
                {
                    if(k==0)
                        printf("%d",idx);
                    else
                        printf(" %d",idx);
                    h[idx]=a;
                    flag=true;
                    break;
                }
            }
            if(flag==false)
            {
                if(k==0)
                    printf("-");
                else
                    printf(" -");
            }
        }
    }
    return 0;
}



PAT 1079 Total Sales of Supply Chain

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

代码:

#include<iostream>
#include<string>
#include<cstdio>
#include<cstring>
#include<vector>
#include<set>
#include<cmath>
#include<algorithm>
using namespace std;
struct node
{
    int num;//货物数量
    int high;//高度
    vector<int> sons;
}nodes[100005];
void dfs(int idx)
{
    int len=nodes[idx].sons.size();
    for(int i=0,j;i<len;++i)
    {
        j=nodes[idx].sons[i];
        nodes[j].high=nodes[idx].high+1;
        dfs(j);
    }
}
int main()
{
    int n,a,b;
    double p,r,summ=0;
    scanf("%d%lf%lf",&n,&p,&r);
    r=1+r/100;
    nodes[0].high=0;
    for(int i=0;i<n;++i)
    {
        scanf("%d",&a);
        if(a==0)
        {
            scanf("%d",&nodes[i].num);
        }
        else
        {
            for(int j=0;j<a;++j)
            {
                scanf("%d",&b);
                nodes[i].sons.push_back(b);
                nodes[i].num=0;
            }
        }
    }
    dfs(0);
    for(int i=0;i<n;++i)
        if(nodes[i].num!=0)
        {

            summ+=(pow(r,nodes[i].high)*nodes[i].num);
            //printf("%d %d %d\n",i,nodes[i].high,nodes[i].num);
        }
    printf("%.1f\n",summ*p);
    return 0;
}



PAT 1080 Graduate Admission

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

代码:


你可能感兴趣的:(PAT Advanced Level (1077~1080))