IDENTITY V (wlacm competition questions)

topic description

Yuki has recently become obsessed with a game called IDENTITY V (Fifth Personality). The Children’s Day is approaching, and the game also launched an event. During the event, use different characters to participate in the battle. According to the player’s performance in the game, the corresponding deduction points will be given to the character. When the deduction points reach 2000, they can be exchanged for the dynamic avatar of the corresponding character. Typical existing characters in the game are as follows:

Yuki really wants to get dynamic avatars, each player can only get one character’s dynamic avatar, if a character has enough points, he will exchange them immediately. Please see which character’s avatar Yuki can get according to Yuki’s battle situation during the event.

enter

Enter a number T (T≤7) in the first line, indicating the number of days Yuki has played this game. Next is the T group of data.

The first line of each set of data is an integer n (n ≤ 20), indicating the number of rounds played that day, followed by n lines of data, each line of data contains a string s (length not exceeding 20) representing the use of Yuki authorities and an integer g (g < 1000) representing the deductive value obtained by Yuki.

output

If Yuki can get all the rewards of a character, output the character; if he can’t get the rewards of any character, output “NOTHING” (without quotation marks)

sample input

【Test sample 1】
5
2
Doctor 500
Gardener 600
2
Doctor 500
Gardener 600
3
Machinist 600
Doctor 500
Machinist 800
1
Doctor 500
1
Machinist 800

[Test example 2, please pay attention to the role name of this group]
3
2
ZXY 900
YWH 500
1
CXW 400
1
WSY 600

sample output

【Test sample 1】
doctor

【Test sample 2】
NOTHING

Tucao: This question is really born! ! ! I have never played the fifth personality, and I don’t know what characters are in it. After reading the question again, I think it is quite simple, but I handed it in several times and it was partially wrong. Later, I finally found out that the question gave 12 The characters are just examples! ! In addition to these 12, there are countless characters, that is to say, even if you lose a shit and reach 2000 points, the shit will still be output, really shit! !

On the train of thought:

Here first define 3 arrays, and their functions have been explained in the comments.

char characters[100][20] = { 0 };//This is the character library
int Score[100] = { 0 };//This is the corresponding score for each character
char thefirst[1][20] = { 0 };//I will store the first character who reaches 2000 points here

Then each time the character name (c) and score (score) are entered, it is first judged whether the character exists in the character library.

char c[20] = { 0 };
int score;
scanf("%s %d", &c, &score);
int which = isbeing(c, role_count);
int isbeing(char* c, int role_count) {
for (int i = 0; i < role_count; i ++ ) {
if (strcmp(characters[i], c) == 0)
return i;//The i returned represents the number of character in the character library
}
return -1;
}//This function is used to determine whether the input role exists in the role library

Here is an isbeing function, which passes the input string (role name) and the number of roles in the current role library (role_count is initialized to 0)

Use the for loop to traverse to find out whether the passed character name is the same in the character library, and return its subscript (here i) to facilitate the calculation of the corresponding score in the Score (point library) later, because the score is like this There is a one-to-one correspondence between the subscripts of the library Score and the subscripts of the character library characters.

Obviously, no matter who the first character you input is, it does not exist in the character library, so it must return -1 for the first time (if it does not exist, it returns 0, this 0 represents the role in the character library What about the first character, or does it not exist? It is confusing, so if it does not exist here, it must be designed to return -1)

Then I mentioned this char* c, which is the formal parameter of the function isbeing, because c is an array (I use it to store the name of the character input each time, so when passing parameters, c is the array name, which is also the beginning of the array. Element address, the address must of course be accepted by a pointer variable, so char* c is a pointer pointing to the address of the first element of the array c, of course, if you are not familiar with pointers, you can also write it as char c[20], 20 is the length of the role name limit) .

① When the character does not exist:

 else {//Not in the role library: the corresponding score is added first, and then added to the role library, role_count is the number of roles plus one
Score[role_count] += score;
strcpy(characters[role_count], c);
role_count++;
}

The characters that do not exist will be added to the character library, and the corresponding points will also be added, and then the number of characters will be accumulated.

Because role_count is initialized to 0, the first new role is placed in the first position of the character library (characters) (because the subscript is 0), and then role_count ++, so the next new character should be placed in the character library The second position, and so on, is in line with expectations and very convenient!

The main point here should be that the string is copied, and the string cannot be assigned to other arrays simply with “=”! Please use strcpy to complete the copy, so also pay attention to the header file #include

②The existence of the role:

 if (which != -1) {//The input role is already in the role library: add the corresponding score
Score[which] += score;
if (Score[which] >= 2000) {
printf("%s", characters[which]);
return 0;
}
}

If it exists, just add points normally, and then write a conditional judgment. When a character reaches 2000 points, immediately output the name of the character and end the program. Note that the subscripts of the score database Score and the subscripts of the character library characters are both One-to-one correspondence.

The question that may arise here is: Once a character reaches the 2000 point level, it will end directly. Is this okay? Doesn’t the point of the character still count?

My answer is that anyway, the question does not require you to calculate the total points of each role, so finish it early and save trouble, which can reduce the time complexity of the program somewhat.

Full code:

#include <stdio.h>
#include <string.h>
char characters[100][20] = { 0 };//This is the character library
int Score[100] = { 0 };//This is the corresponding score for each character

int isbeing(char* c, int role_count) {
for (int i = 0; i < role_count; i ++ ) {
if (strcmp(characters[i], c) == 0)
return i;//The returned i represents the number of character in the character library
}
return -1;
}//This function is used to determine whether the input role exists in the role library
int main()
{
int i, j, role_count = 0;
int T, n;
scanf("%d", &T);
for (i = 0; i < T; i ++ ) {
scanf("%d", &n);
for (j = 0; j < n; j ++ ){
char c[20] = { 0 };
int score;
scanf("%s %d", &c, &score);
int which = isbeing(c, role_count);
if (which != -1) {//The input character is already in the character library: add the corresponding score
Score[which] += score;
if (Score[which] >= 2000) {
printf("%s", characters[which]);
return 0;
}
}
else {//Not in the role library: the corresponding score is added first, and then added to the role library, role_count is the number of roles plus one
Score[role_count] += score;
strcpy(characters[role_count], c);
role_count++;
}
}
}
printf("NOTHING");
return 0;
}

The knowledge points of the article match the official knowledge files, and you can further learn relevant knowledge