0
点赞
收藏
分享

微信扫一扫

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

北邮郭大宝 2022-02-19 阅读 29

暴躁小丁开始准备刷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;
}

继续(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;
}
举报

相关推荐

0 条评论