c#

17 - number letter counts

If the numbers 1 to 5 are written out in words: one, two, three, four, five, then there are 3 + 3 + 5 + 4 + 4 = 19 letters used in total.

If all the numbers from 1 to 1000 (one thousand) inclusive were written out in words, how many letters would be used?

NOTE: Do not count spaces or hyphens. For example, 342 (three hundred and forty-two) contains 23 letters and 115 (one hundred and fifteen) contains 20 letters. The use of “and” when writing out numbers is in compliance with British usage.

cevap : 21124

link

yardimci olmasi icin sayilarin oldugu text dosyasi notes.txt

Derleme ortami Visual studio community c#

// project euler problem 17
// yasin tasan --> apr 2016

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

namespace project_euler_17
{
    class Program
    {   
        static void Main(string[] args)
        {
            Console.WriteLine("Give me a number and i calculate the sum of the total letter used");
            Console.WriteLine("while writing until it");
            Console.WriteLine("");
            Console.WriteLine("For Ex, total letter number will be 19 if you give me 5 --> ");
            Console.WriteLine("one = 3, two = 3, three = 5, four = 3, five = 4 --> total is 19");
            Console.WriteLine("");
            Console.WriteLine("i dont count spaces or hyphens");
            Console.WriteLine("342 (three hundred and forty-two) will be 23");
            Console.WriteLine("and 115 (one hundred and fifteen) will be 20");
            Console.WriteLine("");
            Console.WriteLine("so give me the number now.");
            Console.WriteLine("but dont pass one thousand!");

            UInt32 MaxVal = Convert.ToUInt32(Console.ReadLine());

            UInt32[] Ones = new UInt32[9] { 3, 3, 5, 4, 4, 3, 5, 5, 4 }; // between 1-9
            UInt32[] OnesTens = new UInt32[10] { 3, 6, 6, 8, 8, 7, 7, 9, 8, 8 }; // between 10-19
            UInt32[] Tens = new UInt32[8] { 6, 6, 5, 5, 5, 7, 6, 6 }; // 20 30 40 50 60 70 80 90 
            UInt32[] Hundreds = new UInt32[9] { 10, 10, 12, 11, 11, 10, 12, 12, 11 }; // 100 200 300 400 500 600 700 800 900 
            UInt32 And = 3;
            UInt32 OneThousand = 11;

            UInt32 Sums = 0;
            UInt32 i;

            for (i = 1; i <= MaxVal; i++ )
            {
                if (i < 10) // smaller then 10
                {
                    Sums += Ones[i-1]; 
                }

                if ((i >= 10) && (i < 20)) // between 10-20
                {
                    Sums += OnesTens[i % 10];
                }

                if ( ( (i%10) == 0) && (i >= 20) && (i < 100) ) // between 10-100 and multiples of 10
                {
                    Sums += Tens[i/10-2];
                }

                if ( ( (i % 10) != 0 ) && (i >= 20) && (i < 100) ) // between 10-100 and inter-vals of the multiples of 10
                {
                    Sums += Tens[i / 10 - 2];
                    Sums += Ones[i%10-1];
                }

                if ( ( (i%100) == 0) && (i < 1000) ) // between 100-1000 and multiples of 100
                {
                    Sums += Hundreds[i/100-1];
                }

                if ( (i>100) && ( (i % 100) != 0 ) && (i < 1000) ) // between 100-1000 and in-tervals of the multiples of 100
                {
                    Sums += Hundreds[(i / 100) - 1]; 
                    Sums += And;

                    if ( (i%100) < 10) // smaller then 10
                    {
                        Sums += Ones[(i%100)-1]; 
                    }

                    if (((i%100) >= 10) && ((i%100) < 20)) // between 10-20
                    {
                        Sums += OnesTens[(i%100) % 10];
                    }

                    if ( ( ((i%100)%10) == 0) && ((i%100) >= 20) && ((i%100) < 100) ) // between 10-100 and multiples of 10
                    {
                        Sums += Tens[(i%100)/10-2];
                    }

                    if ( ( ((i%100) % 10) != 0 ) && ((i%100) >= 20) && ((i%100) < 100) ) // between 10-100 and intervals of the multiples of 10
                    {
                        Sums += Tens[(i%100) / 10 - 2];
                        Sums += Ones[(i%100)%10-1];
                    }

                    if ( ( ((i%100)%100) == 0) && ((i%100) < 1000) ) // between 100-1000 and mul-tiples of 100
                    {
                        Sums += Hundreds[(i%100)/100-1];
                    }

                }

                if ( i == 1000 ) // 1000
                {
                    Sums += OneThousand;
                }

            }

            Console.WriteLine("result is {0} for {1}", Sums, MaxVal);
            Console.WriteLine("");
            Console.WriteLine("press any key to continue ...");
            Console.ReadKey();      
        }
    }
}