PAT (Basic Level) Practice C++解题过程(暴躁版)

暴躁小丁开始准备刷PAT,虽然咱是菜鸡一个,整个一摸眼黑,但是有勇气谁都了不起,记录开始,这是一个浙江大学的刷题网站,有兴趣的宝宝可以和俺老丁一起!
链接:https://pintia.cn/problem-sets/994805260223102976/problems/type/7

1001 害死人不偿命的(3n+1)猜想

哈哈,这个名字取得也太搞笑了,害死人不偿命,哈哈哈
这个还可以写个while循环就可以了,我这代码写的总觉得水平太低了,写了好多行

#include <iostream>
using namespace std;
int  main(){
    int n;
    cin >> n;
    int res = 0;
    while(n != 1){
        res++;
        if(n % 2 == 0){
            n = n / 2;
        } else{
            n = (3 * n + 1) / 2;
        }
    }
    cout << res;
    return 0;
}

1002 写出这个数

这个题有20分,所以我们也不难想象应该难度会高一点。
我开始的时候一直想着一个大小为10^10的整数改用什么类型表示呢,如果用int肯定不行,但是float和double由没办法求余,可把我急坏了,结果呢,就想着尝试一下,把输入改成string,这下大小啥的都没问题了,没想到竟然真的可以。
不过我发现,这个系统有个自定义输入输出的功能真的不错,在线编程体验非常好!

#include<iostream>
#include <string.h>
using namespace std;
int main(){
    string n;
    cin >> n;
    int res = 0;
    for(int i = 0 ; i < n.length() ; i++){
        res = res + n[i] -'0';
    }
    string s = to_string(res);
    string num[10] = {"ling","yi","er","san","si","wu","liu","qi","ba","jiu"};
    cout << num[s[0]-'0'];
    for(int i = 1 ; i < s.length() ; i++){
        cout << ' ' << num[s[i]-'0'];
    }
    return 0;
}

1003 我要通过!

这一题真的写的太暴躁了,真的看不懂题目再说啥,人家样例里面的APAAATAA,多好一串字符串,到底是哪里不满足题意,为啥给人家个NO,这不是欺负老实人吗?总之看的我云里雾里,题目中搞得那几个a,b,c也完全不知所云,把题目写清楚一点好吗?
啊,我刚刚去看了看别的博主的解题思路,原来是这个意思,【大哭】,果然我是笨蛋,【大哭】。

#include<iostream>
#include<string.h>
using namespace std;
int main(){
    int n;
    cin >> n;
    for(int i = 0 ; i < n ; i++){
        string s;
        cin >> s;
        int a1 = 0, a2 = 0, a3 = 0;
        int j = 0;
        int flag = 0;
        while(s[j] == 'A' && j < s.length()){
            a1++;
            j++;
        }
        if(s[j] != 'P'){
            flag = 1;
        }
        j++;
        while(s[j] == 'A' && j < s.length()){
            a2++;
            j++;
        }
        if(s[j] != 'T'){
            flag = 1;
        }
        j++;
        while(s[j] == 'A' && j < s.length()){
            a3++;
            j++;
        }
        if(a1 > a3 || a2 == 0 || a1 * a2 != a3){
            flag = 1;
        }
        if(flag == 1){
            cout << "NO" << endl;
        }else{
            cout << "YES" << endl;
        }
    }
    return 0;
}

1004 成绩排名

一个非常正常的题,很开心,嘿嘿

#include<iostream>
#include<string.h>
using namespace std;
int main(){
    int n;
    cin >> n;
    string name, nmin, nmax, id, imin, imax;
    int grade, gmin, gmax;
    cin >> nmin >> imin >> gmin; 
    nmax = nmin;
    imax = imin;
    gmax = gmin;
    for(int i = 1 ; i < n ; i++){
        cin >> name >> id >> grade;
        if(grade > gmax){
            nmax = name;
            imax = id;
            gmax = grade;
        }
        if(grade < gmin){
            nmin = name;
            imin = id;
            gmin = grade;
        }
    }
    cout << nmax << ' ' << imax << endl;
    cout << nmin << ' ' << imin << endl;
    return 0;
}

1005 继续(3n+1)猜想

害死人不偿命的(3n+1)猜想竟然还出了2.0版本,幸好也是个比较正常的题目,就是最后搞格式搞了很久,输出最后不能加空格,结果最后又洋洋洒洒写了好多行代码来解决这个问题。

#include<iostream>
#include<unordered_set>
#include <vector>
#include<algorithm>
using namespace std;
int main(){
    int n;
    cin >> n;
    vector<int> num(n);
    unordered_set<int> set;
    for(int i = 0 ; i < n ; i++){
        cin >> num[i];
        int temp = num[i];
        while(temp != 1){
            if(temp % 2 == 1){
                temp = (3 * temp + 1) / 2;
            }else{
                temp = temp / 2;
            }
            set.insert(temp);
        }
    }
    sort(num.begin(), num.end());
    int i ;
    for(i = n - 1 ; i >= 0 ; i--){
        if(set.count(num[i]) == 0){
            cout << num[i] ;
            i--;
            break;
        }
    }
    for(; i >= 0 ; i--){
        if(set.count(num[i]) == 0){
            cout << ' ' << num[i] ;
        }
    }
    return 0;
}

1006 换个格式输出整数

看到这题分数是15,心里已经开始嘿嘿了,复杂题我唯唯诺诺,简单题我重拳出击

#include<iostream>
#include<string.h>
using namespace std;
int main(){
    int n;
    cin >> n;
    for(int i = 0 ; i < n / 100 ; i++){
        cout << 'B';
    }
    for(int i = 0 ; i < (n / 10) % 10 ; i++){
        cout << 'S';
    }
    for(int i = 1 ; i <= n % 10 ; i++){
        cout << i;
    }
    cout << endl;
    return 0;
}

1007 素数对猜想

救命,我这眼睛估计是有点问题了,我看题目写着“不超过”,但是我脑子中一直是不包含该对象,结果提交的时候,有一个怎么都是错的。【大哭】,有人相爱,有人夜里看海,有人一道简单题抠了一下午才抠出来。

#include<vector>
#include<iostream>
using namespace std;
bool sushu(int n){
    if(n < 4){
        return true;
    }
    int i = 2;
    while(i * i < n){
        if(n % i == 0){
            break;
        }
        i++;
    }
    return i * i > n;
}
int main(){
    int n;
    cin >> n;
    vector<int> temp;
    for(int i = 1 ; i <= n ; i++){
        if(sushu(i)){
            temp.push_back(i);
        }
    }
    int res = 0;
    for(int i = 1 ; i < temp.size() ; i++){
        if(temp[i] - temp[i-1] == 2){
            res++;
        }
    }
    cout << res << endl;
    return 0;
}

1008 数组元素循环右移问题

现在时间是17:37,我真的快要饿死了,自从回到学校,我每天都饿的厉害。虽然我中午干了一大碗白米饭,但是还没撑四个小时,我就已经肚子开始叫唤了。赶紧写完这个1008去吃饭,凑个吉利点的数字,结束今天的代码生活,可偏偏这么一道题,一直提示“格式错误”,哇,我心想,就饶过我吧,怎么又爆出个格式错误哇!一看题目,原来n和m都可能等于0,那难怪了,你俩好兄弟,要格式有问题一起有问题,赶紧乖乖改了代码。
嵌了几层if循环后,我的代码开始有了曲线,终于不是直杆了。干饭去了!明天再来!

#include<iostream>
using namespace std;
int main(){
    int n, m;
    cin >> n >> m;
    int A[n];
    for(int i = 0 ; i < n ; i++){
        cin >> A[i];
    }
    m = m % n;
    if(m == 0){
        if(n != 0){
            cout << A[0];
            for(int i = 1 ; i < n; i++){
                cout << ' ' << A[i];
            }
        }
        
    }
    else{
        if(n != 0){
            cout << A[n-m];
            for(int i = n - m + 1 ; i < n ; i++){
                cout << ' ' << A[i];
            }
            for(int i = 0 ; i < n - m; i++){
                cout << ' ' << A[i];
            }
        }
    }
    cout << endl;
    return 0;
}

1009 说反话

我真的很喜欢用,每次看到这个vector,我脑子中就会情不自禁地想到维克多,嘿嘿,有点梦幻联动的感觉,不知道有没有人看过《冰上的尤里》,里面的俄罗斯老妖精维克多,嘿嘿,不小心暴露了自己的腐女属性。

#include<iostream>
#include<vector>
using namespace std;
int main(){
    string s;
    vector<string> res;
    while(cin >> s){
        res.push_back(s);
    }
    cout << res[res.size() - 1]; 
    for(int i =res.size() - 2 ; i >= 0  ; i--){
        cout << ' ' << res[i];
    }
    cout << endl;
    return 0;
}

1010 一元多项式求导

一顿操作猛如虎,一看得分错的离谱。不知道是不是只有我这么觉得,这些题目出得好多我都看不懂什么意思,比如说这句《注意“零多项式”的指数和系数都是 0,但是表示为 0 0》,原来他的意思是,最后所有相加如过全是0,要输出“0 0”,我还以为是只要出现指数和系数都为0的就输出“0 0”,【笑哭】

#include<iostream>
#include<vector>
using namespace std;
int main(){
    int n1, n2;
    vector<vector<int>> res; 
    while(cin >> n1 >> n2){
        vector<int> temp;
        if(n2 != 0){
            temp.push_back(n1 * n2);
            temp.push_back(n2 - 1);
            res.push_back(temp);
        }
    }
    if(res.size() > 0){
        cout << res[0][0] << ' ' << res[0][1];
        for(int i = 1 ; i < res.size() ; i++){
            cout<< ' ' << res[i][0] << ' ' << res[i][1];
        }
    }else{
        cout << "0 0";
    }
    return 0;
}

1011 A+B 和 C

这个题看似简单,实则暗藏玄机,虽然 3 个整数 A、B 和 C都是给定区间内的 但是,A和B相加有可能是超过int的表示范围,所以要用double,真的到处都是坑啊,头脑简单的我真的是寸步难行

#include<iostream>
using namespace std;
int main(){
    int n;
    cin >> n;
    double a, b, c;
    for(int i = 0 ; i < n ; i++){
        cin >> a >> b >> c;
        if(a + b > c){
            cout << "Case #" << i + 1 << ": " << "true" << endl;
        }else{
            cout << "Case #" << i + 1 << ": " << "false" << endl;
        }
    }
    return 0;
}

1012 数字分类

这道题真的写的我百思不得其解,尤其是这个样例2,这明显8%5=3啊。为啥答案是N呢,这不是欺负我眼神不好吗?我真的是实在不能理解,我找了同学一起来看,她也不能理解,我们两个人用手机自带计算器计算8%5,结果就是3啊,感觉自己的世界观马上就被颠覆了,结果同学又看了一遍题目,告诉我,前面这个8指的是总共输入的数字个数,并不包含在内。【尴尬】【笑哭】,看来我是真的眼神不好【笑哭】
在这里插入图片描述

#include<iostream>
#include"stdio.h"
using namespace std;
int main(){
    int i = 0, a1 = 0, a2 = 0, a3 = 0, a5 = 0;
    double a4 = 0;
    int f1 = 0, f2 = 0, f3 = 0, f4 = 0, f5 = 0;
    int x;
    int flag = -1;
    cin >> x;
    while(cin >> x){
        if(x % 10 == 0){
            f1++;
            a1 = a1 + x;
        }
        else if(x % 5 == 1){
            f2++;
            flag = flag * (-1);
            a2 = a2 + flag * x;
        }
        else if(x % 5 == 2){
            f3++;
            a3++;
        }
        else if(x % 5 == 3){
            f4++;
            i++;
            a4 = a4 + x;
        }
        else if(x % 5 == 4){
            f5++;
            a5 = max(a5, x);
        }
    }
    if(f1 == 0){
        cout << 'N' << ' '; 
    }else{
        cout << a1 << ' ';
    }
    if(f2 == 0){
        cout << 'N' << ' '; 
    }else{
        cout << a2 << ' ';
    }
    if(f3 == 0){
        cout << 'N' << ' '; 
    }else{
        cout << a3 << ' ';
    }
    if(f4 == 0){
        cout << 'N' << ' '; 
    }else{
        printf("%.1f ",a4 / i);
    }
    if(f5 == 0){
        cout << 'N'; 
    }else{
        cout << a5;
    }
    return 0;
}

1013 数素数

刚刚去吃饭,我和室友说,这个PAT题目意思怎么感觉写的很不明确,让人摸不着头脑,她说,有没有一种可能,只有你一个人这么觉得,打击 !【大哭】,幸运的是中午买到了最后一碗辛拉面,辛拉面好吃!好吃!
这道题有一个坑就是最后不能多输出空格或者换行,如果正好有10的倍数个数,要注意最后只能有一个换行。

#include<iostream>
#include<vector>
using namespace std;
bool sushu(int n){
    if(n < 4){
        return true;
    }
    int i = 2;
    while(i * i < n){
        if(n % i == 0){
            break;
        }
        i++;
    }
    return i * i > n;
}
int main(){
    int m, n;
    cin >> m >> n;
    vector<int> res;
    int i = 1;
    int flag = 0;
    while(flag <= n){
        if(sushu(i)){
            flag++;
            if(flag > m){
                res.push_back(i);
            }
        }
        i++;
    }
    cout << res[0];
    for(int i = 1 ; i < res.size() ; i++){
        if(i % 10 == 9){
            if(i == res.size() - 1){
                cout << ' ' << res[i];
            }
            else{
                cout << ' ' << res[i] << endl;
            }
        }
        else if(i% 10 == 0){
            cout << res[i];
        }else{
            cout << ' ' << res[i];
        }
    }
    cout << endl;
    return 0; 
}

1014 福尔摩斯的约会

这一题真的写得很暴躁,因为一直是部分通过,我写了很久,快一个小时了,还是部分通过,实在是不行了,不知道究竟是为啥错,但我感觉,我要是继续写下去,真的得被这道题逼疯。我真心有个建议,应该把部分错误的样例贴出来,要不然真的是黑灯瞎火,伸手不见五指,网恋既视感,多点真诚,多点爱。

#include<iostream>
using namespace std;
int main()
{   
    string a,b,c,d;
    int flag=0;
    string week[7]={"MON","TUE","WED","THU","FRI","SAT","SUN"};
    cin>>a>>b>>c>>d;
    int length1=a.length()>b.length()?b.length():a.length();
    for(int i=0;i<length1;i++)
    {   //寻找第一个相同的大写字母,因为对应的是星期几,所以只找前7个即可 
        if(a[i]==b[i]&&(a[i]>=65&&a[i]<=71)&&flag==0)
        {
            flag=1;
            cout<<week[a[i]-65]<<" ";
            i++;
        }//注意找第二对相同的字符的时候,是从第一队相同大写字母后面找
        //因为样例中8相同,但是他却说E是相同的字符,其次该字符必须在
        //0-9或者A-N之间才行。 
        if(flag==1&&a[i]==b[i])
        {
            if(a[i]>='0'&&a[i]<='9')
            {
                cout<<"0"<<a[i]-'0'<<":";
                break;
            }
            if(a[i]>='A'&&a[i]<='N')
            {
                cout<<a[i]-65+10<<":";
                break;
            }   
        }
    }
    int length2=c.length()>d.length()?d.length():c.length(); 
    for(int i=0;i<length2;i++)
    {   //后两字符串必须满足是两个英文字母相等才可以 
        if(c[i]==d[i]&&((c[i]>=65&&c[i]<=90)||(c[i]>=97&&c[i]<=122)))
        {
            if(i<=9)    //不足两位数补0 
            {
                cout<<"0"<<i<<endl;
                    break;
            }else{
                cout<<i<<endl;
                    break;
            }
        }
    }
    return 0;   
}

最近不刷PAT了,有这时间不如刷leetcode,真是莫名其妙!

11015 德才论

不刷PAT是不可能的,尤其是马上就要机试了,我身边的小伙伴PAT都刷完了,我才做了几道题,于是,我又回来了。这道题倒不复杂,就是判断多了一点,然后写一个compare函数,用algorithm里面的sort函数就可以排序了。

#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;
struct students{
    string id;
    int de;
    int cai;
    students(string i, int d,int c):id(i), de(d), cai(c) {}
};
int compare(students s1, students s2){
    if(s1.de+s1.cai == s2.de+s2.cai){
        if(s1.de == s2.de){
            return s1.id < s2.id;
        }
        return s1.de >= s2.de;
    }
    return s1.de+s1.cai >= s2.de+s2.cai;
}
int main(){
    int n, low, high;
    cin >> n >> low >> high;
    string sid;
    int d, c;
    vector<students> vec1, vec2, vec3, vec4;
    for(int i = 0 ; i < n ; i++){
        cin >> sid >> d >> c;
        if(d >= high && c >= high){
            vec1.push_back(students(sid, d, c));
        }
        else if(c >= low && d >= high){
            vec2.push_back(students(sid, d, c));
        }
        else if(c >= low && d >= low && d >= c){
            vec3.push_back(students(sid, d, c));
        }
        else if(c >= low && d >= low){
            vec4.push_back(students(sid, d, c));
        }
    }
    sort(vec1.begin(), vec1.end(), compare);
    sort(vec2.begin(), vec2.end(), compare);
    sort(vec3.begin(), vec3.end(), compare);
    sort(vec4.begin(), vec4.end(), compare);
    cout << vec1.size() + vec2.size() + vec3.size() + vec4.size() << endl;
    for(int i = 0 ; i < vec1.size() ; i++){
        cout << vec1[i].id << ' ' << vec1[i].de << ' ' << vec1[i].cai << endl;
    }
    for(int i = 0 ; i < vec2.size() ; i++){
        cout << vec2[i].id << ' ' << vec2[i].de << ' ' << vec2[i].cai << endl;
    }
    for(int i = 0 ; i < vec3.size() ; i++){
        cout << vec3[i].id << ' ' << vec3[i].de << ' ' << vec3[i].cai << endl;
    }
    for(int i = 0 ; i < vec4.size() ; i++){
        cout << vec4[i].id << ' ' << vec4[i].de << ' ' << vec4[i].cai << endl;
    }
    return 0;
}

1016 部分A+B

简单题,用string格式遍历一遍就好

#include<iostream>
using namespace std;
int main(){
    string a, b;
    char a1, b1;
    cin >> a >> a1 >> b >> b1;
    int temp1= 0, temp2 = 0;
    for(int i = 0 ; i < a.size() ; i++){
        if(a[i] == a1){
            temp1 = temp1*10 + a1 - '0';
        }
    }
    for(int i = 0 ; i < b.size() ; i++){
        if(b[i] == b1){
            temp2 = temp2*10 + b1 - '0';
        }
    }
    cout << temp1 + temp2 << endl;
    return 0;
}

1017 A除以B

简单题,注意有可能除出来之后商为0的情况。

#include<iostream>
#include<vector>
using namespace std;
int main(){
    string str;
    int x;
    cin >> str >> x;
    vector<int> res;
    
    int i = str[0] - '0' >= x ? 0 : 1;
    int flag = str[0] - '0' >= x ? 0 : str[0] - '0';
    for(i ; i < str.size() ; i++){
        int temp = flag * 10 + str[i] - '0';
        res.push_back(temp / x);
        flag = temp - (temp / x) * x;
    }
    if(res.size() == 0){
        cout << 0;
    }
    for(int i = 0 ; i < res.size() ; i++){
        cout << res[i];
    }
    cout << ' ' << flag << endl;
    return 0;
}

1018 锤子剪刀布

暴力,非常暴力,如今我也变成一个非常暴力的人了

#include<iostream>
#include<map>
using namespace std;
int main(){
    int n;
    cin >> n;
    int js = 0, jp = 0, jb = 0;
    map<char,int> mapj, mapy;
    char a, b;
    for(int i = 0 ; i < n ; i++){
        cin >> a >> b;
        if(a == b){
            jp++;
        }
        else if((a=='C' && b=='J') || (a=='J' && b=='B') || (a=='B' && b=='C')){
            js++;
            mapj[a]++;
        }
        else{
            jb++;
            mapy[b]++;
        }
    }
    cout << js << ' ' << jp << ' ' << jb << endl;
    cout << jb << ' ' << jp << ' ' << js << endl;
    if(mapj['B'] >= mapj['C'] && mapj['B'] >= mapj['J']){
        cout << 'B';
    }
    else if(mapj['C'] >= mapj['B'] && mapj['C'] >= mapj['J']){
        cout << 'C';
    }
    else {
        cout << 'J';
    }
    if(mapy['B'] >= mapy['C'] && mapy['B'] >= mapy['J']){
        cout << ' ' << 'B' << endl;
    }
    else if(mapy['C'] >= mapy['B'] && mapy['C'] >= mapy['J']){
        cout << ' ' << 'C' << endl;
    }
    else {
        cout << ' ' << 'J' << endl;
    }
    return 0;
}

1020 月饼

没错!跳过了1019,为啥捏,因为我不管怎么做都是部分正确,但是我真的找不到原因,害!
这道题倒是还挺简单的,我明天就要机试了【大哭】,【慌】,【吃手手】

#include<iostream>
#include<vector>
#include<algorithm>
#include<cstdio>
using namespace std;
int compare(vector<double> vec1, vector<double> vec2){
    return vec1[1] > vec2[1];
}
int main(){
    int n;
    double v;
    cin >> n >> v;
    vector<vector<double>> vec(n, vector<double>(2));
    for(int i = 0 ; i < n ; i++){
        cin >> vec[i][0];
    }
    for(int i = 0 ; i < n ; i++){
        cin >> vec[i][1];
        vec[i][1] = vec[i][1] / vec[i][0];
    }
    sort(vec.begin(), vec.end(), compare);
    double res = 0;
    for(int i = 0 ; i < n && v > 0; i++){
        double temp = v;
        v = max(v - vec[i][0], 0.0);
        res = res + (temp - v) * vec[i][1];
    }
    printf("%.2lf", res);
    return 0;
}

1021 个位数统计

我爱简单题!

#include<iostream>
#include<vector>
using namespace std;
int main(){
    string str;
    cin >> str;
    vector<int> nums(10, 0);
    for(int i = 0 ; i < str.size() ; i++){
        nums[str[i] - '0']++;
    }
    for(int i = 0 ; i < 10 ; i++){
        if(nums[i] > 0){
            cout << i << ':' << nums[i] << endl;
        }
    }
    return 0;
}

1022 D进制的A+B

有一个坑,就是如果这两个数都是0,应该是输出‘0’,一定要注意!

#include<iostream>
using namespace std;
int main(){
    int a, b, d;
    cin >> a >> b >> d;
    int res = a + b;
    if(a + b == 0){
        cout << '0';
        return 0;
    }
    string str;
    while(res > 0){
        str = to_string(res % d) + str;
        res = res / d;
    }
    cout << str << endl;
    return 0;
}

1023 组个最小数

简单题yyd!重拳出击!

#include<iostream>
#include<vector>
using namespace std;
int main(){
    vector<int> nums;
    int x;
    string str;
    for(int i = 0 ; i < 10 ; i++){
        cin >> x;
        for(int j = 0 ; j < x ; j++){
            str = str + to_string(i);
        }
    }
    if(str[0] == '0'){
        int i = 0;
        while(str[i] == '0'){
            i++;
        }
        char temp = str[i];
        str[i] = '0';
        str[0] = temp;
    }
    cout << str << endl;
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

猫头丁

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值