公式

A - Legendary Players 解説 by en_translator


For beginners

This problem asks to determine which of the usernames the input coincides, and print the corresponding rating (actually, one can distinguish the user only by the first character.)

For example, the following approaches are possible:

  • Using a conditional branch
    • Sample code: C++ and Haskell
  • Using an associative array
    • Sample code: Python
  • Using a string replacement
    • Sample code: sed

1. Using a conditional branch

The problem can be solved by inspecting each of the usernames one by one and print the corresponding rating if it coincides with the input.

The following are sample codes.

C++

#include <iostream>
#include <string>

using namespace std;

int main() {
    string s;
    cin >> s;
    
    if (s == "tourist")
        cout << 3858 << endl;
    if (s == "ksun48")
        cout << 3679 << endl;
    if (s == "Benq")
        cout << 3658 << endl;
    if (s == "Um_nik")
        cout << 3648 << endl;
    if (s == "apiad")
        cout << 3638 << endl;
    if (s == "Stonefeang")
        cout << 3630 << endl;
    if (s == "ecnerwala")
        cout << 3613 << endl;
    if (s == "mnbvmar")
        cout << 3555 << endl;
    if (s == "newbiedmy")
        cout << 3516 << endl;
    if (s == "semiexp")
        cout << 3481 << endl;
    
    return 0;
}

Haskell

main = interact (\s -> case s of
    "tourist" -> "3858"
    "ksun48" -> "3679"
    "Benq" -> "3658"
    "Um_nik" -> "3648"
    "apiad" -> "3638"
    "Stonefeang" -> "3630"
    "ecnerwala" -> "3613"
    "mnbvmar" -> "3555"
    "newbiedmy" -> "3516"
    "semiexp" -> "3481")

2. Using an associative array

Some languages come with a feature that can find the value associated to a given string. If C++, it can be done with std::map; in Python, with dict
One can use this to associate the usernames with the ratings, in order to solve this problem.

The following is a sample code.

username_to_rating = {
    "tourist": "3858", 
    "ksun48": "3679", 
    "Benq": "3658", 
    "Um_nik": "3648", 
    "apiad": "3638", 
    "Stonefeang": "3630", 
    "ecnerwala": "3613", 
    "mnbvmar": "3555", 
    "newbiedmy": "3516", 
    "semiexp": "3481"
}

print(username_to_rating[input()])

Also, one can split the given string to construct an associative array with the dict comprehension syntax.

data = """tourist 3858
ksun48 3679
Benq 3658
Um_nik 3648
apiad 3638
Stonefeang 3630
ecnerwala 3613
mnbvmar 3555
newbiedmy 3516
semiexp 3481"""

username_to_rating = {name: rating for name, rating in map(lambda s: s.split(), data.splitlines())}

print(username_to_rating[input()])

3. Using a string replacement

One can apply a replacement against the input from usernames to the corresponding rating.

The following is a sample code.

sed

s/tourist/3858/
s/ksun48/3679/
s/Benq/3658/
s/Um_nik/3648/
s/apiad/3638/
s/Stonefeang/3630/
s/ecnerwala/3613/
s/mnbvmar/3555/
s/newbiedmy/3516/
s/semiexp/3481/

投稿日時:
最終更新: