PTA Vegetable Chicken Diary (1-5)

L1-001 Hello World (5分)

There is no input for this super simple question.

You only need to output the famous phrase "Hello World!" on one line.

#include <iostream>
using namespace std;
int main(){
    
    
    cout<<"Hello World!"<<endl;
    return 0;
}

L1-002 printing hourglass (20 points)

This question requires you to write a program to print the given symbol into the shape of an hourglass.

The so-called "hourglass shape" means that each line outputs an odd number of symbols; the centers of the symbols in each line are aligned; the number of symbols in two adjacent lines is different by 2; the number of symbols first decreases from largest to smallest to 1, and then increases from smallest to largest; first and last symbols The numbers are equal.

Given any N symbols, it does not necessarily constitute an hourglass. It is required that the printed hourglass can use as many symbols as possible.

Input format:
Input one positive integer N (≤1000) and one symbol in one line, separated by spaces.

Output format:
First print out the largest hourglass shape composed of the given symbols, and finally output the number of unused symbols in one line.

#include <iostream>
using namespace std;

int main(){
    
    
    int i = 0;
    char c;
    cin>>i>>c;
    int sum = 1, t = 1;
    for(int j = 1; (sum + j + 2) * 2 - 1 <= i; j += 2){
    
    
        t = j + 2;
        sum = sum + j + 2;
    } //t:最顶层的数量, sum:需要的符号数总和
    //上层
    for(int k = t; k >= 1; k -= 2){
    
    
        if(k < t){
    
    
            for(int a = 0; a < (t - k)/2; a++){
    
    
                cout<<' ';
            }
            for(int a = 0; a < k; a++){
    
    
                cout<<c;
            }
            cout<<endl;
        }
        else{
    
    
            for(int a = 0; a < k; a++){
    
    
                cout<<c;
            }
            cout<<endl;
        }
    }
    //下层
    for(int k = 3; k <= t; k += 2){
    
    
        if(k < t){
    
    
            for(int a = 0; a < (t - k)/2; a++){
    
    
                cout<<' ';
            }
            for(int a = 0; a < k; a++){
    
    
                cout<<c;
            }
            cout<<endl;
        }
        else{
    
    
            for(int a = 0; a < k; a++){
    
    
                cout<<c;
            }
            cout<<endl;
        }
    }
    //剩余符号数
    cout<<i - sum * 2 + 1;
    return 0;
}

L1-003 Single-digit statistics (15 points)

Given a k-digit integer N, please write a program to count the occurrences of each different single digit.
For example: Given N=100311, there are 2 0s, 3 1s, and 1 3.

Input format:
Each input contains 1 test case, that is, a positive integer N with no more than 1000 digits.

Output format:
For each different one digit in N, output the digit D and the number of occurrences M in N in a line in the format of D:M. It is required to output in ascending order of D.

#include <iostream>
#include <vector>
#include <cstring>
using namespace std;
int main(){
    
    
    char str[1000];
    cin>>str; 
    int l = strlen(str);
    vector <int> res(10, 0);
    for(int j = 0; j < l; j++){
    
    
        switch(str[j] - 48){
    
    
            case 0: res[0]++; break;
            case 1: res[1]++; break;
            case 2: res[2]++; break;
            case 3: res[3]++; break;
            case 4: res[4]++; break;
            case 5: res[5]++; break;
            case 6: res[6]++; break;
            case 7: res[7]++; break;
            case 8: res[8]++; break;
            default: res[9]++;
        }
    }
    for(int a = 0; a < 10; a++){
    
    
        if(res[a] != 0) cout<<a<<':'<<res[a]<<endl;
    }
    return 0;
}

L1-004 Calculate Celsius temperature (5 points)

Given a Fahrenheit temperature F, this question requires writing a program to calculate the corresponding Celsius temperature C. Calculation formula: C=5×(F−32)/9. The question guarantees that the input and output are within the integer range.

Input format:
Input gives a temperature in Fahrenheit in one line.

Output format:
output the corresponding integer value of Celsius temperature C in one line according to the format "Celsius = C".

#include <iostream>
using namespace std;
int main(){
    
    
    int F = 0;
    cin>>F;
    int C = (F - 32) * 5 / 9;
    cout<<"Celsius = "<<C<<endl;
    return 0;
}

L1-005 Exam seat number (15 points)

Each PAT candidate will be assigned two seat numbers when taking the test, one is the test seat and the other is the test seat. Under normal circumstances, candidates first get the test machine seat number when entering the venue. After entering the test machine state, the system will display the test candidate's test seat number. During the test, the candidate needs to change to the test seat. But some candidates are late and the test machine is over. They can only ask you for help with the test machine seat number they received, and find out their test seat number from the backstage.

Input format:
Input the first line to give a positive integer N (≤1000), and then N lines, each line gives one candidate's information: admission ticket number, test machine seat number, test seat number. The admission ticket number consists of 16 digits, and the seats are numbered from 1 to N. Enter to ensure that everyone's admission ticket number is different, and no two people will be assigned to the same seat at any time.

After the candidate's information, give a positive integer M (≤N), and then give the M test seat numbers to be queried in a row, separated by spaces.

Output format:
corresponding to each test machine seat number that needs to be queried, output the test admission ticket number and test seat number of the corresponding candidate in one line, separated by a space.

#include <iostream>
#include <vector>
using namespace std;
struct Student{
    
    
    long long num;
    int e1;
    int e2;
};
int main(){
    
    
    int a = 0;
    cin >> a;
    vector <Student> res(a);
    for(int i = 0; i < a; i++){
    
    
        cin >> res[i].num >> res[i].e1 >> res[i].e2;
    }
    int b = 0;
    cin >> b;
    vector <int> match(b);
    for(int j = 0; j < b; j++){
    
    
        cin >> match[j];
    }
    for(int k = 0; k < b; k++){
    
    
        for(int i = 0; i < a; i++){
    
    
            if(res[i].e1 == match[k]) cout << res[i].num << ' ' << res[i].e2 << endl;
        }
    }
    return 0;
}

Guess you like

Origin blog.csdn.net/m0_46203495/article/details/108615905