Problem B 1475 Ice pier and curling

Directory

topic

Ice pier and curling

topic description

input format

output format

sample input

sample output

Idea analysis

Distinguish between ” Win ” & amp; ” Lose “

Calculate points

code design

Functions that need to be implemented

variable required

code modularization

Data needs to be initialized

function

input function

Judgment/calculation/output function

main function

submit code

Harvest and summary

For the use of pointers

Tips about entering characters

Ideas before optimization, optimization points and WA points


Title

Ice and Curling

Acceted : 234

Submit : 769

Time Limit : 1000 MS

Memory Limit : 65536 KB

Title Description

Curling is known as “chess on ice”. Its scoring rule is to throw each stone. Finally, in the base camp, how many stones you have are closer to the center of the circle than all the opponent’s stones are to the center of the circle. For example, the red side has two pots, which are at coordinates (1,1), (?2,1); the yellow side also has two pots, which are at (1,0), (0,2). Since the yellow side’s first pot is closer to the center of the circle, and the yellow side’s second pot is not as close as the red side’s first pot, the yellow side gets 1 point and the red side gets no points.

Now Bingdundun chooses the red side every time, it wants to know how many points it has scored?

Input Format

The first line is an integer T (1≤T≤3000), indicating the number of samples.

The first line of each sample is an integer n (1≤n≤16), indicating the number of curling stones. Afterwards, each line contains information about a curling stone, consisting of three parts. The first one is a character indicating the color of the curling stone (R means red, Y means yellow), followed by two integers (the absolute value of the value does not exceed 1000) , indicating the curling coordinates.

The input data guarantees that no two jugs will appear at the same coordinates.

Output Format

Output the results for each example in turn. The result is one line, which is divided into two parts. If the opponent does not score, output “Win” and the score, otherwise output “Lose” and the score scored by the opponent, with a space between them.

Sample input

2

4

Y 1 1

R -2 1

R 1 0

Y 0 2

4

Y 1 1

R 0 1

R 1 0

Y 0 2

Sample output

Win 1

Win 2

Thinking Analysis

Distinguish ” Win ” & amp; ” Lose “

Compare the d (the closest distance from the center of the circle) of the two teams

calculated score

For the winning team, compare the curling distance with the losing team d

The easy mistake here is to mistakenly think that the number of curling stones in the two teams is equal, which is half of the total number of curling stones

Code Design

Function to be implemented

Accurately capture and store input data

Calculate the center distance of each team

Get the minimum center distance of each team

judge the winning team

Calculate the winning team’s final score

required variable

Only for input and thinking analysis, other indirect application variables are added in each module

Number of samples T

The total number of curling n

Curling coordinates x , y

The number of curling stones in each team cnty , cntr

The curling center distance of each team Y[20] , R[20]

The minimum curling center distance of each team miny , minr

Winning team score score

Code Modularization

Need to initialize data

int T;
//int n; dispensable in main(), also available as a local variable
//The following variables are initialized inside the sample loop
int Y[20] = {0};
int R[20] = {0};
int cnty = 0;
int cntr = 0;
int miny = 3000000;
int minr = 3000000;
//int score = 0; Here, the judging function and the output function are merged, so as a local variable, it is directly calculated and output

Functional function

//Functional functions (the ones that do not apply other functions) are generally recommended to be written before all functions
int Distance(int a, int b){
    return a * a + b * b;
}//Calculate the distance between the center of the circle, for the convenience of comparison, the square root will not be processed

Input function

Obtain the minimum center-to-center distance while obtaining the input data

void Printin(int *Y, int *R, int *cnty, int *cntr, int *miny, int *minr){
    int n;//Because n is only useful when storing data, it appears as a local variable here
    scanf("%d", &n);
    getchar();//absorb extra carriage returns
    while(n--){
        char team;
        int x, y;
        scanf("%c %d %d", &team, &x, &y);
        getchar();//absorb extra carriage returns
        if(team == 'Y'){
            *(Y + *cnty) = Distance(x, y);
            *miny = (*(Y + *cnty) < *miny) ? *(Y + *cnty) : *miny;//Minimum center distance
            (*cnty) + + ;
        }
        else if(team == 'R'){
            *(R + *cntr) = Distance(x, y);
            *minr = (*(R + *cntr) < *minr) ? *(R + *cntr) : *minr;//minimum center-to-center distance
            (*cntr) + + ;
        }
    }
}

judgment/calculation/output function

When storing data, you can record the number of data and take the maximum or minimum value by the way

It can also be output by the way when judging

void Judge(int *Y, int *R, int *cnty, int *cntr, int *miny, int *minr){
    int score = 0;
    if(*miny < *minr){//Judge the winning team, if they are equal, they will win, that is, neither of them will score, and the R team will win according to the meaning of the question
        printf("Lose ");
        for(int i = 0;i < *cnty;i ++ ){
            if(*(Y + i) < *minr) score + + ;//Calculate the winning team score
        }
        printf("%d\\
", score);
    }
    else {
        printf("Win");
        for(int i = 0;i < *cntr;i ++ ){
            if(*(R + i) < *miny) score + + ;//Calculate the winning team score
        }
        printf("%d\\
", score);
    }
}

main function

int main(){
    int T;
    scanf("%d", &T);

    while(T--){
        int Y[20] = {0};
        int R[20] = {0};
        int cnty = 0;
        int cntr = 0;
        int miny = 3000000;
        int minr = 3000000;

        Printin(Y, R, &cnty, &cntr, &miny, &minr);
        Judge(Y, R, &cnty, &cntr, &miny, &minr);
    }

    return 0;
}

Submit code

Problem: 1475 User: strong>202105501225
Memory: 1184K Time: 31MS
Language: G + + Result: Accepted
#include <stdio.h>

int Distance(int a, int b){
    return a * a + b * b;
}

void Printin(int *Y, int *R, int *cnty, int *cntr, int *miny, int *minr){
    int n;
    scanf("%d", &n);
    getchar();
    while(n--){
        char team;
        int x, y;
        scanf("%c %d %d", &team, &x, &y);
        getchar();
        if(team == 'Y'){
            *(Y + *cnty) = Distance(x, y);
            *miny = (*(Y + *cnty) < *miny) ? *(Y + *cnty) : *miny;
            (*cnty) + + ;
        }
        else {
            *(R + *cntr) = Distance(x, y);
            *minr = (*(R + *cntr) < *minr) ? *(R + *cntr) : *minr;
            (*cntr) + + ;
        }
    }
}

void Judge(int *Y, int *R, int *cnty, int *cntr, int *miny, int *minr){
    int score = 0;
    if(*miny < *minr){
        printf("Lose ");
        for(int i = 0;i < *cnty;i ++ ){
            if(*(Y + i) < *minr) score + + ;
        }
        printf("%d\\
", score);
    }
    else {
        printf("Win");
        for(int i = 0;i < *cntr;i ++ ){
            if(*(R + i) < *miny) score + + ;
        }
        printf("%d\\
", score);
    }
}

int main(){
    int T;
    scanf("%d", &T);

    while(T--){
        int Y[20] = {0};
        int R[20] = {0};
        int cnty = 0;
        int cntr = 0;
        int miny = 3000000;
        int minr = 3000000;

        Printin(Y, R, &cnty, &cntr, &miny, &minr);
        Judge(Y, R, &cnty, &cntr, &miny, &minr);
    }

    return 0;
}

Harvest and summary

Using pointers

A deeper understanding of the relationship between pointers and arrays

The pointer to the array involved here is the name of the array (pointing to the first address of the array)

So at this time, when declaring the pointer variable, it is int *Y (not int *Y[] or others)

The variable transferred when using the function is also the array name directly

Tips about entering characters

You can try it, after deleting the getchar() in the input function

What happens to the characters entered into the character variable team?

The most intuitive feeling is that the result is output before the data is input

The reason is that at this time %c read the carriage return of the previous scanf()

There are several solutions:

  • Use getchar() after the previous scanf() of %c (there are two scanf()s that need to be added in this function)
  • Add a space at the end of the previous scanf() of %c or ‘ \\
    ‘or ‘ \t ‘ (two places)
  • Add a space before %c or ‘ \\
    ‘or ‘ \t ‘ (I saw it on other people’s solution, it feels the most convenient)

Still digesting the role of spaces in scanf

I found two explanations that feel okay

can be combined to see

[C language] Scanf statement eats the carriage return or space problem – what does scanf eat the carriage return?_Z Xiaoxuan’s Blog-CSDN Blog

Why add a space before the scanf function %c_For scanf(), %c_hhhhhyyyyy8’s blog-CSDN Blog

Before optimization ideas and optimization points and WA points

becomes

syntaxbug.com © 2021 All Rights Reserved.