Introduction training for beginners of Niuke network programming 2

Posted by Popcorn on Wed, 19 Jan 2022 13:40:28 +0100

BC11 student basic information input and output

describe

Input a student's student number and 3 subjects (C language, mathematics, English) scores in turn, and output the student's student number and 3 subjects' scores on the screen (Note: the scores shall be rounded and 2 decimal places shall be reserved).

Data range: student number meets 1 < = n < = 20000000 1 <= n <= 20000000 1 < = n < = 20000000, the score of each subject uses the percentage system, and there can be no negative number

Enter Description:

Student number and grades of 3 subjects are separated by English semicolons and English commas.

Output Description:

Student number, grade of 3 subjects. See the output example for the output format.

Example 1

Input:

17140216;80.845,90.55,100.00

Output:

The each subject score of No. 17140216 is 80.85, 90.55, 100.00.

Example 2

Input:

123456;93.33,99.99,81.20

Output:

The each subject score of No. 123456 is 93.33, 99.99, 81.20.

Inner thoughts:

At first glance, it's just input and output. What's wrong? After handing in the code in twos and threes, you get a WA... When you look at the question carefully, you find that the input data is separated by English semicolons and English commas, rather than the default space, Tab and newline characters. Ignoring this problem will lead to zero except the first output result. I haven't paid attention to this problem before. I have to study hard this time.

Fortunately, this question is more conscientious. The input group of data is all numbers. We can use one character to receive the separator.

For other information, please refer to this blog:

[c + +] how to use comma as separator when inputting the value of variable with cin

The code implementation is as follows:

#include<iostream>
#include<cmath>
#include<iomanip>
using namespace std;
int main()
{
    int a;
    double b,c,d;
    char sep;
    cin>>a>>sep>>b>>sep>>c>>sep>>d;
    b=round(b*100)/100;
    c=round(c*100)/100;
    d=round(d*100)/100;
    cout<<"The each subject score of No. "<<a<<" is ";
    cout<<setprecision(2)<<fixed;
    cout<<b<<", "<<c<<", "<<d<<"."<<endl;
}

There are also two points to note:

First, the output results should be rounded. There are different rounding methods. Here, the round() function is used, which is contained in the header file:

#include<cmath>

As for the rounding of decimals, check out this short blog:

Rounding of c + + decimals

Second, the output results should keep two decimal places. This requires the parameterized flow operator setprecision(). The program should include this header file:

#include<iomanip>

The data output after line 15 is two decimal places.

BC12 character Christmas tree

describe

Enter a character and use it to construct a triangular Christmas tree with side length of 5.

Data range: ensure that the input character is a char type value

Enter Description:

The input has only one line and one character.

Output Description:

This character forms a triangular Christmas tree.

Example 1:

Input:

1

Output:

    1
   1 1
  1 1 1
 1 1 1 1
1 1 1 1 1

Inner thoughts:

At first glance, I think it should be a common and basic chessboard like question, and the idea is relatively clear. Take example input as an example, that is, take a 1 and a space after it as a whole, and then find the relationship between the number of spaces before the first 1 of each line and the number of lines.

It can be seen from this figure that there are n yellow blocks in line n, and the number of spaces before the first 1 in this line meets 5-n (n here) 1 < = n < = 5 1 <= n <= 5 1<=n<=5 ). Naturally, the cycle comes out. The code implementation is as follows:

#include<iostream>
using namespace std;
int main()
{
    char ch;
    cin>>ch;
    for(int i = 0;i < 5;i++)
    {
        for(int n = 4-i;n > 0;n--)
        {
            cout<<" ";
        }
        for(int j = 0;j <= i;j++)
        {
            cout<<ch<<" ";
        }
        cout<<endl;
    }
}

The number of lines in this implementation is specified to start from 0. This kind of problem often leads to PE because you don't pay attention to spaces and line breaks, and it will waste a lot of time to check, which is very uneconomical. You should consider these before you start.

BC13 ASCII code

describe

BoBo teaches KiKi that characters represented by character constants or character variables are stored in ASCII code in memory. BoBo has a problem with KiKi. Convert the following ASCII codes into corresponding characters and output them.

73, 32, 99, 97, 110, 32, 100, 111, 32, 105, 116 , 33

Enter Description:

nothing

Output Description:

Convert all ASCII characters given in the output title to the corresponding characters.

Inner thoughts:

We have tried to forcibly convert a character to an integer, but we have not tried to convert an integer to a corresponding character. We reasonably speculate that since (int)ch is used to forcibly convert a character to an integer (where ch refers to a character), is it possible to use (char)num (where num refers to the ASCII code of a character) to convert an integer to a corresponding character. Try again.

Because there are twelve ASCII codes, each one defines sub children that are not very smart. Let's use the array. The implementation of purchasing agent is as follows:

#include<iostream>
using namespace std;
int main()
{
    int arr[] = {73,32,99,97,110,32,100,111,32,105,116,33};
    cout<<(char)arr[0]<<(char)arr[1]<<(char)arr[2]<<(char)arr[3]<<(char)arr[4]<<(char)arr[5]<<(char)arr[6]<<(char)arr[7]<<(char)arr[8]<<(char)arr[9]<<(char)arr[10]<<(char)arr[11]<<endl;
}

A very simple paragraph, directly AC.

In fact, it can be simpler, although it would appear that my reasonable guess is stupid······

#include<iostream>
using namespace std;
int main()
{
    char arr[] = {73,32,99,97,110,32,100,111,32,105,116,33};
    cout<<arr[0]<<arr[1]<<arr[2]<<arr[3]<<arr[4]<<arr[5]<<arr[6]<<arr[7]<<arr[8]<<arr[9]<<arr[10]<<arr[11]<<endl;
}

When you define an array, you can directly output it by directly defining it as a char type······

BC14 date of birth input / output

describe

Enter a person's birth date (including year, month and day), and output the year, month and day of the birthday respectively.

Data range: year satisfied 1990 < = y < = 2015 1990 <= y <= 2015 1990 < = y < = 2015, month satisfied 1 < = m < = 12 1 <= m <= 12 1 < = m < = 12, satisfied on the day 1 < = d < = 30 1 <= d <= 30 1<=d<=30

Enter Description:

There is only one line to enter. The date of birth includes year, month and day. There is no separator between year, month and day.

Output Description:

Three lines, the first line is the year of birth, the second line is the month of birth, and the third line is the date of birth. When outputting, if the month or day is 1 digit, you need to fill 0 in front of 1 digit.

Example 1:

Input:

20130225 

Output:

year=2013
month=02
date=25

remarks:

adopt scanf Functional%m Format control can specify the input field width and input the data field width (number of columns), and intercept the required data according to this width; adopt printf Functional%0 Format control character. When outputting numerical value, specify the empty position not used on the left, and fill in 0 automatically.

Inner thoughts:

You want me to write in C, right? It's impossible. I must use cout output format.

There are three test points for this question:

One is to intercept each part from a string of input data without separator. This is quite common. It is nothing more than some operations of modulus, addition, subtraction, multiplication and division. There are many methods, just choose one;

Second, the control domain is wide, which is also very common. You can use the parameterized flow operator setw(). Don't forget the header file;

Third, add 0 before the month or day with 1 digit. This is something I haven't noticed before. Here, setfill() is used to set the padding character.

For the knowledge of zero filling before and after, please refer to this blog:

C++ cout format output complement 0

The code implementation is as follows:

#include<iostream>
#include<iomanip>
using namespace std;
int main()
{
    int birth;
    cin>>birth;
    int year = birth / 10000;
    int month = birth / 100 - year * 100;
    int date = birth - birth / 100 * 100;
    cout<<"year="<<year<<"\n"<<"month="<<setw(2)<<setfill('0')<<month<<"\n"<<"date="<<setw(2)<<setfill('0')<<date<<endl;
}

Note that the statements of setting the field width and filling in zeros are only valid for the first subsequent output data and are one-time. Moreover, the zero padding in setfill() is filled with single quotation marks ('') to specify 0, so if you replace it with other characters, it will be filled with other characters.

Topics: C++ Programming