PAT (Advanced Level) Practice甲级练习

今天开始PTA刷题记录,文章持续更新~
对于比较难的和有坑的题会有讲解

文章目录

1001 A+B Format (20 分)

#include<iostream>

using namespace std;
int s[10];
int main(){
    int a,b;
    cin>>a>>b;
    int sum=a+b;
    
    bool A=0;
    if(sum<0){
        sum*=(-1);
        A=1;
    }
    if(sum==0) cout<<0;
    int n=0;
    while(sum){
        s[++n]=sum%10;
        sum/=10;
    }
    if(A) cout<<"-";
    for(int i=n;i;i--){
        cout<<s[i];
        if(i==n-n%3+1&&n>=4) cout<<",";
        if((i-(n-n%3+1))%3==0&&i/4>0&&i!=n-n%3+1) cout<<",";
    }
    
    return 0;
}

1002 A+B for Polynomials (25 分)

#include<iostream>
#include<stdio.h>
using namespace std;

double ans[1010]={0};
bool v[10100]={false};

int main(){
    int k1,k2;
    cin>>k1;
    int a;
    double k;
    int num=0;
    for(int i=0;i<k1;i++){
        cin>>a>>k;
        if(!v[a]&&k!=0.0){v[a]=1;num++;}
        ans[a]+=k;
    }
    cin>>k2;
    for(int i=0;i<k2;i++){
        cin>>a>>k;
        if(!v[a]&&k!=0.0){v[a]=1;num++;}
        ans[a]+=k;
    }
   // cout<<v[0]<<endl;
    cout<<num;
    for(int i=1000;i>=0;i--){
        if(ans[i]!=0 ) printf(" %d %.1f", i,ans[i]);
    }
    return 0;
}

1005 Spell It Right (20 分)

#include<iostream>
using namespace std;

void Eng(int x){
    if(x==0) cout<<"zero";
    else if(x==1) cout<<"one";
    else if(x==2) cout<<"two";
    else if(x==3) cout<<"three";
    else if(x==4) cout<<"four";
    else if(x==5) cout<<"five";
    else if(x==6) cout<<"six";
    else if(x==7) cout<<"seven";
    else if(x==8) cout<<"eight";
    else cout<<"nine";
}
int main(){
    string s;
    cin>>s;
    int n=s.size();
    int sum=0;
    for(int i=0;i<n;i++){
        sum+=(int)(s[i]-'0');
    }
    int mm=0,summ=sum;
    
    while(summ){summ/=10;mm++;}
    int m=1;
    for(int i=1;i<mm;i++) m*=10;
    //cout<<sum<<" "<<m<<endl;
    while(sum||m){
        int k=sum/m;
        Eng(k);
        sum=sum%m;
        m/=10;
        if(m) cout<<" ";
    }
    return 0;
}

1006 Sign In and Sign Out (25 分)

#include <iostream>

using namespace std;

int main(){
    string open_id,open_time;
    string close_id,close_time;
    
    int m;
    cin >> m;
    for(int i = 0;i < m ; i ++){
         string id, in_time , out_time;
        cin >> id >> in_time >> out_time;
        
        if(!i || open_time > in_time) {
            open_time = in_time;
            open_id = id;
        }
        
        if(!i || close_time < out_time) {
            close_time = out_time;
            close_id = id;
        }
    }
    cout << open_id << ' ' << close_id;
}

1007 Maximum Subsequence Sum (25 分)(最大子序列和)

//最大子序列和
#include<iostream>
using namespace std;

int a[100086];
int main(){
    int n;
    cin>>n;
    int m=0;
    for(int i=0;i<n;i++){
        cin>>a[i];
        if(a[i]<0) m++;
    }
    if(m==n){
        cout<<0<<" "<<a[0]<<" "<<a[n-1];
        return 0;
    }
    long long maxx=a[0],l=a[0],r=a[0];
    for(int i=0;i<n;i++){
        int s=0;
        for(int j=i;j<n;j++){
           s+=a[j];
          if(s>maxx){
              maxx=s;
              l=a[i];
              r=a[j];
          }
          
        }
    }
    cout<<maxx<<" "<<l<<" "<<r<<endl;
    return 0;
}

1008 Elevator (20 分)

#include<iostream>
using namespace std;

int a[10086];
int main(){
    int n;
    cin>>n;
    long long sum=n*5;
    for(int i=0;i<n;i++){
        cin>>a[i];
        if(!i) sum+=6*a[i];
        else{
            if(a[i]>a[i-1]) sum+=(a[i]-a[i-1])*6;
            else if(a[i]==a[i-1]) sum+=0;
            else sum+=(a[i-1]-a[i])*4;
        }
    }
    cout<<sum<<endl;
}

1009 Product of Polynomials (25 分)

#include<iostream>
using namespace std;

int ae[10086],be[10086];
double ac[10086],bc[10086];

double ans[10086];
int main(){
    int n,m;
    cin>>n;
    for(int i=0;i<n;i++) cin>>ae[i]>>ac[i];
    cin>>m;
    for(int i=0;i<m;i++) cin>>be[i]>>bc[i];
    for(int i=0;i<m;i++){
        for(int j=0;j<n;j++){
            int sum=0;
            sum=ae[j]+be[i];
            ans[sum]+=ac[j]*bc[i];
          //  cout<<sum<<ac[j]*be[i]<<" "<<ac[j]<<" "<<bc[i]<<endl;
        }
    }
    int k=0;
    for(int i=2000;i>=0;i--){
        if(ans[i]!=0.0) k++;
    }
    cout<<k;
    for(int i=2000;i>=0;i--){
        if(ans[i]!=0.0) printf(" %d %.1lf",i,ans[i]);
    }
    
    return 0;
}

1010 Radix (25 分)(大坑)

#include<iostream>
using namespace std;
#define int long long int
int jinshi(string s,int k){
    int sum=0,m=1;
    for(int i=s.size()-1;i>=0;i--){
        if(isdigit(s[i])){sum+=(int)(s[i]-'0')*m;m*=k;}
        else{sum+=(int)(s[i]-'a'+10)*m;m*=k;}
    }
    return sum;
}

int find(int l,int r,int n,string s){
    int m;
    while(l<=r){
        int mid=(l+r)/2;
        m=jinshi(s,mid);
        //cout<<m<<" "<<n<<" "<<mid<<" "<<s<<endl;
        if(m==n) return mid;
        if(m>n||m<0) r=mid-1;
        else l=mid+1;
    }
    return -1;
}

signed main(){
    string n1,n2;
    int t,r;
    cin>>n1>>n2>>t>>r;
    if(n1==n2){cout<<r;return 0;}
    if(t==2) swap(n1,n2);
    int a=jinshi(n1,r);
    int minn=0,maxx;
    for(int i=0;i<n2.size();i++){
        if(isdigit(n2[i])){
            if((int)(n2[i]-'0'+1)>minn) minn=(int)(n2[i]-'0'+1);
        }else{ 
            if((int)(n2[i]-'a'+1)>minn) minn=(int)(n2[i]-'a'+11);
            //cout<<"z"<<endl;
        }
    }
    maxx=max(minn,a);
   //cout<<maxx<<" "<<minn<<endl;
    int ans=find(minn,maxx,a,n2);
    if(ans==-1) cout<<"Impossible";
    else cout<<ans;
}

1011 World Cup Betting (20 分)

#include<iostream>
#include<algorithm>
using namespace std;

struct sz{
    int n;
    double m;
}a[4];

int cmp(sz x,sz y){
    return x.m>y.m;
}
int main(){
    double sum=1;
    for(int i=0;i<3;i++){
       cin>>a[0].m>>a[1].m>>a[2].m;
       a[0].n=1,a[1].n=2,a[2].n=3;
       sort(a,a+3,cmp);
       sum*=a[0].m;
        if(a[0].n==1) cout<<"W ";
        else if(a[0].n==2) cout<<"T ";
        else cout<<"L ";
    }
    sum=(sum*0.65-1)*2;
    printf("%.2lf",sum);
    return 0;
}

1015 Reversible Primes (20 分)

一直WA了第二组数据,结果是因为,0,1,不是素数。。。

#include<iostream>
#include<cstring>
#include<math.h>
using namespace std;

const int N=10010000;
int str[N],str_n;

void jinzhi(int k,int x){
    while(x){
        int m=x%k;
        str[str_n++]=m;
        x/=k;
    }
    return;
}

int zhuan(int k){
    int sum=0,m=1;
    for(int i=str_n-1;i>=0;i--){
        sum+=str[i]*m;
        m*=k;
    }
    return sum;
}

bool pri(int x){
    //0,1不算质数
    if(x==0||x==1) return false;
    for(int i=2;i<=sqrt(x);i++){
        if(x%i==0) return false;
    }
    return true;
}

int main(){
    int n,d;
    while(cin>>n){
    memset(str,0,sizeof str);
    str_n=0;
    if(n<0) return 0;
    cin>>d;
    if(!pri(n)){cout<<"No"<<endl;continue;}
    jinzhi(d,n);
    int m=zhuan(d);
    //cout<<m<<endl;
    if(!pri(m)){cout<<"No"<<endl;continue;}
    else cout<<"Yes"<<endl;
    }
    return 0;
}
上一篇:PAT (Basic Level) Practice 1002(中文)(C++)


下一篇:PAT (Basic Level) Practice (中文)1009 说反话