Project Euler #22: Names scores

  • + 0 comments

    //C# using System; using System.Collections.Generic;

    class Solution { static void Main(string[] args) { int n = int.Parse(Console.ReadLine()); string[] str = new string[n];

    SortedDictionary<string, int> sorted = new SortedDictionary<string, int>();
    
    for (int i = 0; i < n; i++)
    {
        int sum = 0;
    
        str[i] = Console.ReadLine();
        char[] characters = str[i].ToCharArray();
        foreach (char ch in characters)
        {
            sum += (int)ch - 64; 
        }
        sorted[str[i].ToUpper()] = sum;
    }
    
    var entryArray = new KeyValuePair<string, int>[sorted.Count];
    sorted.CopyTo(entryArray, 0);
    
    for (int i = 0; i < sorted.Count; i++)
    {
        int newNumber = entryArray[i].Value * (i + 1);
        sorted[entryArray[i].Key] = newNumber;
    }
    
    int q = int.Parse(Console.ReadLine());
    
    for (int i = 0; i < q; i++)
    {
        string name = Console.ReadLine();
        if (sorted.ContainsKey(name.ToUpper()))
        {
            Console.WriteLine(sorted[name.ToUpper()]);
        }
    }
    

    } }