PAT (Basic Level) Practice C + + problem solving process (grumpy version)

Grumpy Xiaoding began to prepare to brush PAT. Although I am a vegetable chicken and feel black at the touch of the whole, anyone with courage is great. At the beginning of the record, this is a brush question website of Zhejiang University. Interested babies can work with me!
Link: https://pintia.cn/problem-sets/994805260223102976/problems/type/7

1001 (3n+1) conjecture that killing people does not pay for their lives

Haha, it's funny to get this name. Killing people doesn't pay for their lives, haha
This can also write a while loop. I always think the level of code writing is too low. I wrote many lines

#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 write this number

This question has 20 points, so it's not difficult for us to imagine that it should be a little more difficult.
At the beginning, I always thought about what type to use to represent an integer with the size of 10 ^ 10. If I use int, it certainly won't work, but I was worried because I couldn't find the balance of float and double. As a result, I wanted to try and change the input to string. Now there's no problem with the size. I didn't expect that it could really work.
However, I found that this system has a user-defined input and output function, which is really good, and the online programming experience is very good!

#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 I want to pass!

This question is really too irritable. I really can't understand what to say about the topic. What a good string of apaataa in other people's sample. Where is it that doesn't meet the meaning of the question? Why give others a NO? Isn't it bullying honest people? In short, I'm in a fog. I don't know what a, b and c in the title are. Will you write the title clearly?
Ah, I just went to see the problem-solving ideas of other bloggers. It turns out that it means, "cry". Sure enough, I'm a fool, [cry].

#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 score ranking

A very normal question, very happy, hey hey

#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;
}

Continue (3n+1) conjecture

The (3n+1) conjecture that killing people is not worth their lives has also produced version 2.0. Fortunately, it is also a relatively normal problem, that is, it took a long time to make the format, and no spaces can be added to the output. As a result, it finally wrote a lot of lines of code to solve this problem.

#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 output integer in another format

Seeing that the score of this question is 15, my heart has begun to Hei hei. I am submissive to complex questions and strike hard at simple questions

#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 prime pair conjecture

Help, I think there's something wrong with my eyes. I read the title "no more than", but I haven't included the object in my mind. When I submitted the results, one of them was wrong. [crying] some people fall in love, some people watch the sea at night, and some people pick out a simple question all afternoon.

#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 circular right shift of array elements

Now it's 17:37. I'm really starving. I've been hungry every day since I came back to school. Although I cooked a large bowl of white rice at noon, my stomach began to cry before I could last four hours. Hurry to finish writing this 1008, go to dinner, gather a lucky number, and end today's code life. However, such a problem always prompts "format error". Wow, I thought, just spare me. How can I burst another format error WOW! When you look at the title, it turns out that both n and M may be equal to 0. No wonder, your two good brothers, if there is a problem with the format and the same problem, quickly change the code.
After embedding several layers of if loops, my code began to have a curve, and finally it was not a straight bar. Dry dinner! Come back tomorrow!

#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;
}

Keywords: C++ Back-end

Added by cillosis on Sun, 20 Feb 2022 12:10:36 +0200