PAT-Basic刷题1011-1020

1011 A+B 和 C

#include
#include
using namespace std;
int main(void){
    int n;
    cin>>n;
    for(int i=1;i<=n;i++){
        long long int a,b,c;
        scanf("%lld%lld%lld",&a,&b,&c);
        printf("Case #%d: %s\n",i,a+b>c?"true":"false");
    }
    return 0;
}

1012 数字分类

#include
#include
#include
using namespace std;
int main(void){
    int a1=0,a2=0,a5=0;
    double a4=0.0;
    int n,num;
    cin>>n;
    vector<int> v[5];
    for(int i=0;i<n;i++){
        cin>>num;
        v[num%5].push_back(num);
    }
    for(int i=0;i<5;i++){
        for(int j=0;j<v[i].size();j++){
            if(i==0&&v[i][j]%2==0) a1+=v[i][j];
            if(i==1&&j%2==0) a2+=v[i][j];
            if(i==1&&j%2==1) a2-=v[i][j];
            if(i==3) a4+=v[i][j];
            if(i==4&&v[i][j]>a5) a5=v[i][j];
        }
    }
    for(int i=0;i<5;i++){
        if(i!=0) cout<<" ";
        if((i==0&&a1==0)||(i!=0&&v[i].size()==0)){
            cout<<"N";continue;
        }
        if(i==0) cout<<a1;
        if(i==1) cout<<a2;
        if(i==2) cout<<v[2].size();
        if(i==3) printf("%.1f",a4/v[3].size());
        if(i==4) cout<<a5;
    }
    return 0;
}

1013 数素数

#include
#include
#include
using namespace std;
bool prime(int n){
    for(int i=2;i*i<=n;i++)
        if(n%i==0) return false;
    return true;
}
int main(void){
    int m,n,num=2;
    cin>>m>>n;
    int count=0;
    vector<int> v;
    while(count<n){
        if(prime(num)){
            count++;
            if(count>=m) v.push_back(num);
        }
        num++;
    }
    count=0;
    for(int i=0;i<v.size();i++){
        count++;
        if(count%10!=1) cout<<" ";
        cout<<v[i];
        if(count%10==0) cout<<endl;
    }
    return 0;
}

1014 福尔摩斯的约会

#include
#include
#include
using namespace std;
int main(){
    string a,b,c,d;
    cin>>a>>b>>c>>d;
    int i=0,j=0;
    int t1=0,t2=0,t3=0;
    string week[7]={"MON","TUE","WED","THU","FRI","SAT","SUN"};
    while(i<a.length()&&i<b.length()){
        if(a[i]==b[i]&&a[i]>='A'&&a[i]<='G'){
            t1=a[i]-'A';
            break;
        }
        i++;
    }
    i++;
    while(i<a.length()&&i<b.length()){
        if((a[i]==b[i])&&((a[i]>='A'&&a[i]<='N')||isdigit(a[i]))){
            t2=isdigit(a[i])?a[i]-'0':(a[i]-'A'+10);
            break;
        }
        i++;
    }
    while(j<c.length()&&j<d.length()){
        if(c[j]==d[j]&&isalpha(c[j])){
            t3=j;
            break;
        }
        j++;
    }
    cout<<week[t1]<<" ";
    printf("%02d:%02d",t2,t3);
    return 0;
}

1015 德才论

#include
#include
#include
using namespace std;
struct Student{
    int num;
    int score1;
    int score2;
};
int cmp(Student a,Student b){
    if((a.score1+a.score2)!=(b.score1+b.score2))
        return (a.score1+a.score2)>(b.score1+b.score2);
    else if(a.score1!=b.score1)
        return a.score1>b.score1;
    else
        return a.num<b.num;
}
int main(){
    int n,low,high;
    cin>>n>>low>>high;
    vector<Student> v[4];
    Student student;
    int s=n;
    for(int i=0;i<n;i++){
        cin>>student.num>>student.score1>>student.score2;
        if(student.score1<low||student.score2<low)
            s--;
        else if(student.score1>=high&&student.score2>=high)
            v[0].push_back(student);
        else if(student.score1>=high&&student.score2<high)
            v[1].push_back(student);
        else if(student.score1<high&&student.score2<high&&student.score1>=student.score2)
            v[2].push_back(student);
        else
            v[3].push_back(student);
    }
    cout<<s<<endl;
    for(int i=0;i<4;i++){
        sort(v[i].begin(),v[i].end(),cmp);
        for(int j=0;j<v[i].size();j++)
            printf("%d %d %d\n",v[i][j].num,v[i][j].score1,v[i][j].score2);
        
    }
    return 0;
}

1016 部分A+B

#include
#include
#include
using namespace std;
int main(){
    string a,b;
    int c,d;
    cin>>a>>c>>b>>d;
    int d1=0,d2=0;
    for(int i=0;i<a.length();i++)
        if(a[i]-'0'==c)
            d1=d1*10+c;
    for(int i=0;i<b.length();i++)
        if(b[i]-'0'==d)
            d2=d2*10+d;
    cout<<d1+d2;
    return 0;
}

1017 A除以B

#include
#include
#include
using namespace std;
int main(){
    string s;
    int a;
    cin>>s>>a;
    int t;
    t=(s[0]-'0')/a;
    if(s.length()==1||(s.length()>1&&t!=0))
        cout<<t;
    int temp=(s[0]-'0')%a;
    for(int i=1;i<s.length();i++){
        t=(temp*10+s[i]-'0')/a;
        cout<<t;
        temp=(temp*10+s[i]-'0')%a;
    }
    cout<<" "<<temp;
    return 0;
}

1018 锤子剪刀布

#include
#include
#include
using namespace std;
int main(){
    int n;cin>>n;
    int win_jia=0,win_yi=0;
    int jia[3]={0},yi[3]={0};
    for(int i=0;i<n;i++){
        char s,t;
        cin>>s>>t;
        if(s=='J'&&t=='B'){
            jia[2]++;win_jia++;
        }else if(s=='C'&&t=='B'){
            yi[0]++;win_yi++;
        }else if(s=='B'&&t=='J'){
            yi[2]++;win_yi++;
        }else if(s=='C'&&t=='J'){
            jia[1]++;win_jia++;
        }else if(s=='B'&&t=='C'){
            jia[0]++;win_jia++;
        }else if(s=='J'&&t=='C'){
            yi[1]++;win_yi++;
        }
    }
    cout<<win_jia<<" "<<n-win_jia-win_yi<<" "<<win_yi<<endl;
    cout<<win_yi<<" "<<n-win_jia-win_yi<<" "<<win_jia<<endl;
    int maxjia=jia[0]>=jia[1]?0:1;maxjia=jia[maxjia]>=jia[2]?maxjia:2;
   int maxyi=yi[0]>=yi[1]?0:1;maxyi=yi[maxyi]>=yi[2]?maxyi:2;
    char str[4]={"BCJ"};
    cout<<str[maxjia]<<" "<<str[maxyi];
    return 0;
}

1019 数字黑洞

#include
#include
#include
#include
using namespace std;
bool cmp(char a,char b){
    return a>b;
}
int main(){
    string s;
    cin>>s;
    s.insert(0,4-s.length(),'0');//补0
    do{
        string a=s,b=s;
        sort(a.begin(),a.end(),cmp);
        sort(b.begin(),b.end());
        int result=stoi(a)-stoi(b);
        s=to_string(result);
        s.insert(0,4-s.length(),'0');
        cout<<a<<" - "<<b<<" = "<<s<<endl;
    }while(s!="6174"&&s!="0000");
    return 0;
}

1020 月饼

#include
#include
#include
using namespace std;
struct mooncake{
    float mount,price,unit;
};
bool cmp(mooncake a,mooncake b){
    return a.unit>b.unit;
}
int main(){
    int n,need;
    cin>>n>>need;
    vector<mooncake> v(n);
    for(int i=0;i<n;i++) cin>>v[i].mount;
    for(int i=0;i<n;i++) cin>>v[i].price;
    for(int i=0;i<n;i++) v[i].unit=v[i].price/v[i].mount;
    sort(v.begin(),v.end(),cmp);
    float result=0.0;
    for(int i=0;i<n;i++){
        if(v[i].mount<=need)
            result+=v[i].price;
        else {
            result+=v[i].unit*need;
            break;
        }
        need-=v[i].mount;
    }
    printf("%.2f",result);
    return 0;
}

你可能感兴趣的:(C++)