събота, 29 декември 2012 г.

Решения (13-14) от C# - Loops

Задача 13


13.Write a program that calculates for given N how many trailing zeros present at the end of the number N!. Examples:
N = 10 -> N! = 3628800 -> 2
N = 20 -> N! = 2432902008176640000 -> 4
Does your program work for N = 50 000?
Hint: The trailing zeros in N! are equal to the number of its prime divisors of value 5. Think why!

using System;
 
class FactorielTrailingZeros
{
    static void Main()
    {
        Console.WriteLine("We will calculate the number of trailing zeros for N!.");
        Console.Write("Enter N:");
        int n;
 
        while ((!int.TryParse(Console.ReadLine(), out n)) || (n < 1))
        {
            Console.Write("Incorrect number, please enter it again:");
        }
 
        int numberTrailingZeros = 0;
        int devisor = 5;
        while (n >= devisor)
        {
            numberTrailingZeros += n / devisor;
            devisor *= 5;
        }
 
        Console.WriteLine("The number of trailing zeros is: {0}", numberTrailingZeros);
    }
}

Задача 14


14.Write a program that reads a positive integer number N (N < 20) from console and outputs in the console the numbers 1 ... N numbers arranged as a spiral.

Example for N = 4











using System;
 
class SpiralArray
{
    static void Main()
    {
        Console.Write("Enter the matrix level N:");
        int n;
 
        while ((!int.TryParse(Console.ReadLine(), out n)) || (n >= 20))
        {
            Console.Write("Incorrect number, please enter it again:");
        }
 
        int [,] spiralArray = new int[n,n];
 
        int x = 0;
        int y = 0;
        int count = 1;
        int xStep = 1;
        int yStep = 0;
        int xLeftBorder = -1;
        int xRightBorder = n;
        int yTopBorder = 0;
        int yBottomBorder = n;
        int tempXDirection = 1;
        int tempYDirection = 1;
 
        while (count <= (n * n))
        {
            spiralArray[x, y] = count;
 
            x += xStep;
            y += yStep;
 
            if ((xStep == 1) && (x == (xRightBorder - 1)))
            {
                xRightBorder--;
                xStep = 0;
                yStep = tempYDirection;
                tempXDirection = -1;
            }
            else if ((xStep == -1) && (x == (xLeftBorder + 1)))
            {
                xLeftBorder++;
                xStep = 0;
                yStep = tempYDirection;
                tempXDirection = 1;
            }
            else if ((yStep == 1) && (y == (yBottomBorder - 1)))
            {
                yBottomBorder--;
                xStep = tempXDirection;
                yStep = 0;
                tempYDirection = -1;
            }
            else if ((yStep == -1) && (y == (yTopBorder + 1)))
            {
                yTopBorder++;
                xStep = tempXDirection;
                yStep = 0;
                tempYDirection = 1;
            }
 
            count++;
        }
 
        for (int i = 0; i < n; i++)
        {
            for (int j = 0; j < n; j++)
            {
                Console.Write("{0,4}", spiralArray[j,i]);
            }
            Console.WriteLine();
        }
    }
}

Няма коментари:

Публикуване на коментар