ZeroJudge O087: Prince's Name

Recently, a prince was born, and the king wants to give him a perfect name. However, it is difficult to come up with a good name on his own, so the king decided to ask the people to submit name suggestions for reference.

Since there are so many people in the country, the number of submissions might be too large to read through. Therefore, the king wants you to write a program to help him. The king has given you a Python function (as shown below), which can help you calculate the score a name will receive in the king's mind. Please use this function to sort the submitted names. (Please sort by the scores in the king's mind from smallest to largest, and if two names have the same score, the one submitted first should come first).

Below is the Python function given to you by the king. When you pass a name (string) to it, it will calculate a score between 0 and 100, representing how well the name is perceived in the king's mind. Note that if the input to this function is not a string, the calculated score will be -1.

def Evaluate(Name: str):                       
    if(type(Name) != str):                     
        return -1                              
                                               
    Score = 0                                  
    NameLen = len(Name)                        
                                               
    for i in range(NameLen):                   
        CharCode = ord(Name[i])                
        Score += ((CharCode * 1123) % 1002)    
                                               
        while (CharCode > 0):                  
            Score += (CharCode % 10)           
            CharCode = (CharCode // 10)        
                                               
    return (Score % 101)                       

Sample Inputs/Outputs

Sample Input(s)Sample Output(s)
The first line of input is a positive integer N, representing the total number of names collected (N ≤ 600).
Following that, there are N lines, each containing a string composed of letters, representing a name. (Each name has a length of up to 15 characters.)
Please sort these names based on their scores in the king's mind. After sorting, output each name followed by its score on a separate line, separated by a space. Therefore, the output will consist of N lines in total.
5
Megumin
GawrGura
chunchunmaru
BeggarCannon
Kazuma
Megumin 17
BeggarCannon 19
GawrGura 50
Kazuma 56
chunchunmaru 88
ZeroJudge O087 Sample Test Case

Thought Process

Here is how the given function would look in C++:

int Evaluate(const string str) {
    int Score = 0;
    for (int i = 0; i<str.length(); i++) {
        int CharCode = float(str[i]);
        Score += CharCode * 1123 % 1002;
        while (CharCode > 0) {
            Score += CharCode % 10;
            CharCode /= 10;
        }
    }
    return Score % 101;
}

Use pairs to collect data: input names, evaluate their scores, and declare an array of type std::pair. Store the score as Pair.first, and the order of input as Pair.second. Declare a string array and store the names in it.

Afterward, directly sort this array. Because it stores the order of input, the individuals who were input first will be placed at the front. Use Pair.second to output the strings.

Sample Code-ZeroJudge O087: Prince's Name

#include <iostream>
#include <algorithm>
using namespace std;

int Evaluate(const string str) {
    int Score = 0;
    for (int i = 0; i<str.length(); i++) {
        int CharCode = float(str[i]);
        Score += CharCode * 1123 % 1002;
        while (CharCode > 0) {
            Score += CharCode % 10;
            CharCode /= 10;
        }
    }
    return Score % 101;
}

int main() {
    cin.sync_with_stdio(0);
    cin.tie(0);
    int N;
    cin >> N;
    pair<int, int> score[1000] = {};
    string name[1000] = {};
    for (int i = 0; i<N; i++) {
        cin >> name[i];
        score[i].first = Evaluate(name[i]);
        score[i].second = i;
    }
    sort(score, score+N);
    for (int i = 0; i<N; i++) {
        cout << name[score[i].second] << " " << score[i].first << "\n";
    }
}

//ZeroJudge O087
//Dr. SeanXD

Comments