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

Решения (9,11) от C# - Conditional Statements

Задача 9

We are given 5 integer numbers. Write a program that checks if the sum of some subset of them is 0. Example: 3, -2, 1, 1, 8 -> 1+1-2=0.

using System;
using System.Collections.Generic;
 
class ZeroSum
{
    //The method returns the sum of all combinations of N elements (N = level)
    //from a list of elements (numbersList)
 
    //This means for example that when level = 2 the method will return the sum
    //of all pairs from the list of elements
    static List <int> RecursiveListSum(int level, List <int> numbersList)
    {
        List<int> numbersSubList = new List<int> (numbersList);
        List<int> numberSubListSum = new List<int> { };
        List<int> numberListSum = new List<int> { };
 
        if (level == 1)
        {
            return numbersList;
        }
        else
        {
            foreach (int numberListItem in numbersList)
            {
                numbersSubList.Remove(numberListItem);
 
                //Gets the sums of all combinations from the lower lever
                //For example when our level is 3 we get one element, then we
                //construct a list with the rest of the items and we get all
                //sums of this items ny pairs(level 2). After we get all this
                //sums we add to each of them "our" current element. This way we
                //get the sum of our element with all possible pairs.
                //After that we repeat with the next element
                numberSubListSum = RecursiveListSum(--level, numbersSubList);
 
                foreach (int subListSumItem in numberSubListSum)
                {
                    numberListSum.Add(numberListItem + subListSumItem);
                }
            }
        }
        return numberListSum;
    }
 
    static void Main()
    {
        byte listLenght = 5;
        List<int> numbers = new List<int> { };
        int tempListVal = 0;
 
        for (int i = 0; i < listLenght; i++)
        {
            Console.WriteLine("Enter number {0}:", i + 1);
 
            while (!int.TryParse(Console.ReadLine(), out tempListVal))
            {
                Console.WriteLine("Incorrect number, please enter it again:");
            }
            numbers.Add(tempListVal);
        }
 
        //We check if some of the elements is 0
        if (numbers.Exists(c => c == 0))
        {
            Console.WriteLine("There is a subset of elements with zero sum!");
        }
        else
        {
            List<int> sumList = new List<int> { };
            int levelCheck = listLenght;
            bool zeroSumFlag = false;
            
            //We build the sums of all combinations of elements
            //First we get the sum of all elements (for example - all 5 elements)
            //Then we get the sum of all combinations of the elements from
            //the lower level (for example 5-1 = 4 : we get all combinations of 4 elements)
            //and so on..
            //When we get a sum that is equal to zero we stop the loop
            while ((levelCheck > 1) && (zeroSumFlag == false))
            {
                sumList = RecursiveListSum(levelCheck, numbers);
                if (sumList.Exists(c => c == 0))
                {
                    zeroSumFlag = true;
                }
                levelCheck--;
            }
 
            if (zeroSumFlag)
            {
                Console.WriteLine("There is a subset of elements with zero sum!");
            }
            else
            {
                Console.WriteLine("There is no zero sum subset!");
            }
        }    
    }
}

Задача 11

Write a program that converts a number in the range [0...999] to a text corresponding to its English pronunciation. Examples:
0 -> "Zero"
273 -> "Two hundred seventy three"
400 -> "Four hundred"
501 -> "Five hundred and one"
711 -> "Seven hundred and eleven"


using System;
 
class ReadNumber
{
    static void Main()
    {
        Console.Write("Enter the your number:");
        int number;
 
        while ((!int.TryParse(Console.ReadLine(), out number)) || (number > 999))
        {
            Console.Write("Incorrect number, please enter it again:");
        }
 
        string numberRead = "";
        string and = "";
        int firstDigit = number % 10;
        int secondDigit = number / 10;
        secondDigit = secondDigit % 10;
        int thirdDigit = number / 100;
        
        if (secondDigit == 1)
        {
            and = "and ";
 
            switch (firstDigit)
            {
                case 0:
                    numberRead = "Ten";
                    break;
                case 1 :
                    numberRead = "Eleven";
                    break;
                case 2:
                    numberRead = "Twelve ";
                    break;
                case 3:
                    numberRead = "Thirteen";
                    break;
                case 4:
                    numberRead = "Fourteen";
                    break;
                case 5:
                    numberRead = "Fifteen";
                    break;
                case 6:
                    numberRead = "Sixteen";
                    break;
                case 7:
                    numberRead = "Seventeen";
                    break;
                case 8:
                    numberRead = "Eighteen";
                    break;
                case 9:
                    numberRead = "Nineteen";
                    break;
            }
        }
        else
        {
            switch (firstDigit)
            {
                case 1:
                    numberRead = "One";
                    break;
                case 2:
                    numberRead = "Two";
                    break;
                case 3:
                    numberRead = "Three";
                    break;
                case 4:
                    numberRead = "Four";
                    break;
                case 5:
                    numberRead = "Five";
                    break;
                case 6:
                    numberRead = "Six";
                    break;
                case 7:
                    numberRead = "Seven";
                    break;
                case 8:
                    numberRead = "Eight";
                    break;
                case 9:
                    numberRead = "Nine";
                    break;
            }
 
            switch (secondDigit)
            {
                case 1:
                    break;
                case 2:
                    numberRead = "Twenty " + numberRead;
                    break;
                case 3:
                    numberRead += "Thirty " + numberRead;
                    break;
                case 4:
                    numberRead += "Fourty " + numberRead;
                    break;
                case 5:
                    numberRead += "Fifty " + numberRead;
                    break;
                case 6:
                    numberRead += "Sixty " + numberRead;
                    break;
                case 7:
                    numberRead += "Seventy " + numberRead;
                    break;
                case 8:
                    numberRead += "Eightty " + numberRead;
                    break;
                case 9:
                    numberRead += "Ninety " + numberRead;
                    break;
                default :
                    if (numberRead != "")
                    {
                        and = "and ";
                    }
                    break;
            }
        }
 
        switch (thirdDigit)
        {
            case 1 :
                numberRead = "One Hundred " + and + numberRead;
                break;
            case 2:
                numberRead = "Two Hundred " + and + numberRead;
                break;
            case 3:
                numberRead = "Three Hundred " + and + numberRead;
                break;
            case 4:
                numberRead = "Four Hundred " + and + numberRead;
                break;
            case 5:
                numberRead = "Five Hundred " + and + numberRead;
                break;
            case 6:
                numberRead = "Six Hundred " + and + numberRead;
                break;
            case 7:
                numberRead = "Seven Hundred " + and + numberRead;
                break;
            case 8:
                numberRead = "Eight Hundred " + and + numberRead;
                break;
            case 9:
                numberRead = "Nine Hundred " + and + numberRead;
                break;
        }
 
        if (number == 0)
        {
            numberRead = "Zero";
        }
 
        Console.WriteLine("Your number is: " + numberRead);
    }
}

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

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