c#

22 - names scores

Using names.txt (right click and ‘Save Link/Target As…’), a 46K text file containing over five-thousand first names, begin by sorting it into alphabetical order. Then working out the alphabetical value for each name, multiply this value by its alphabetical position in the list to obtain a name score.

For example, when the list is sorted into alphabetical order, COLIN, which is worth 3 + 15 + 12 + 9 + 14 = 53, is the 938th name in the list. So, COLIN would obtain a score of 938 × 53 = 49714.

What is the total of all the name scores in the file?

link

cevap : 871198282

Derleme ortami visual studio c#

// project euler problem 22 
// yasin tasan
// august 2017

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace names_scores
{
    class Program
    {
        static void Main(string[] args)
        {
            string names_text = System.IO.File.ReadAllText("names.txt"); // isimleri dosyadan oku 
            string[] seperated_text = names_text.Split(','); // virgul sayesinde isimleri ayir

            // array'deki isimlerden tirnak'lari kaldir
            char[] trim_char = new char[] { '\"' };
            for (int i = 0; i < seperated_text.Length; i++)
            {
                seperated_text[i] = seperated_text[i].Trim(trim_char);
            }

            // string array'den list'e gecis
            List<string> list = new List<string>();
            for (int i = 0; i < seperated_text.Length; i++)
            {
                list.Add(seperated_text[i]);
            }
            // liste alfabetik siralama
            list.Sort();

            UInt32 sum = 0;
            UInt32 product_sum = 0;

            for (int j = 0; j < list.Count; j++)
            {
                for (int i = 0; i < list[j].Length; i++)
                {
                    sum += (UInt32)Convert.ToInt32(list[j][i])-64; // alphabetical worth degeri
                    if (i == (list[j].Length - 1))
                    {
                        product_sum += (UInt32)(sum * (j + 1)); // name score degeri 
                        sum = 0;
                    }
                }
            }
            Console.WriteLine(product_sum); // sonucu yazdir 
        }
    }
}