Saturday, December 29, 2012

Решение на задачите от Първия практически изпит по C# -2012 ( 29.12.2012)


Problem 1 – Triple Rotation of Digits

In Kaspichan we drink a lot. One evening we drunk enough so we invited an interesting game: Someone says a number. The first person after him rotates the digits of this number by moving its last digit at its start (e.g. 12345 -> 51234). The second person after that again rotates the number (e.g. 51234 -> 45123). Finally the third person after him also rotates the number (e.g. 45123 -> 34512). The obtained number then is sent by SMS to a fellow group of alcoholics who continue the game at their drink place.

Write a program that helps the Kaspichan drinkers to calculate the triple digits rotation of given number K. Note that zeroes could also take part in the play and the leading digits are lost after each rotation, e.g. the triple rotation of 180001 is 1180 (180001 -> 118000 -> 011800 -> 11800 -> 01180 -> 1180).

Input


The input data should be read from the console and consists of a single line holding an integer number K.

The input data will always be valid and in the format described. There is no need to check it explicitly.

Output


The output data should be printed on the console.

The output should consist of a single line holding the number obtained after applying a triple digits rotation of the number K.


Constraints


  • The number K is in the range [1…999 999] inclusive.
  • Allowed work time for your program: 0.1 seconds. Allowed memory: 16 MB.

Examples

Input
Output

Input
Output

Input
Output

Input
Output
51234
23451

180001
1180

443
443
53
35

Solution:
using System;

class Problem01
{
    static void Main()
    {
        //variables
        int k = int.Parse(Console.ReadLine());
        string result = null;
        string number;

        //expressions
        number = k.ToString();
        result = number;
        for (int i = 1; i <= 3; i++)
        {
            char lastChar = result[result.Length - 1];
            string trimLastChar = result.Substring(0, result.Length - 1);
            if (lastChar == '0')
            {
                result = trimLastChar;
            }
            else
            {
                result = lastChar + trimLastChar;
            }

        }
        Console.WriteLine(result);
    }
}


Problem 2 – Quadronacci Rectangle


You all know the Fibonacci sequence. Well, the Quadronacci sequence is almost the same, but it uses the last four numbers (instead of the last two) to calculate the next number in the sequence. So, we can define each element in the sequence as:


Q n = Q n-1 + Q n-2 + Q n-3 + Q n-4

where Q n is the current Quadronacci number (n is the index of the current Quadronacci number).

The Quadronacci sequence can begin with any four integer numbers – positive or negative – and continue as described by the formula above.

Now, a Quadronacci rectangle is what you probably expect – a rectangle (matrix) of numbers from the Quadronacci sequence. So we can say that the rectangle's height is actually the number of rows and the rectangle's width is the numbers of columns of numbers.

If use R for the number of rows and C for the number of columns, then the first row in the rectangle contains the first C numbers from the sequence, the second row contains the next C numbers from the sequence and so on.

Your task is to write a program, which prints to the console a Quadronacci rectangle by given the first four numbers of the Quadronacci sequence, the number of rows and the number of columns in the rectangle.

Input


The input data should be read from the console.

The first four lines will contain the values of the first four numbers of the Quadronacci sequence – each number will be on a separate line.

On the fifth line there will be the number R – the number of rows of the Quadronacci rectangle.

On the sixth line there will be the number C – the number of columns of the Quadronacci rectangle.

The input data will always be valid and in the format described. There is no need to check it explicitly.

Output


The output data should be printed on the console.

The output should contain exactly R lines with exactly C numbers per line – representing each line of the Quadronacci rectangle. Numbers should be separated by exactly one whitespace (" "), and there shouldn't be any whitespaces after the last number on a line.

Constraints


  • 1 ≤ R ≤ 20.
  • 4 ≤ C ≤ 20.
  • Any number in the Quadronacci rectangle can be stored in a 64-bit signed integer.

·         Allowed working time for your program: 0.1 seconds.

·         Allowed memory: 16 MB.



Examples

Input example
Output example
1
2
3
4
2
8
1 2 3 4 10 19 36 69
134 258 497 958 1847 3560 6862 13227
5
-5
1
2
3
4
5 -5 1 2
3 1 7 13
24 45 89 171



Solution:
using System;

class Problem02
{
    static void Main()
    {
        //variables
        long number1 = long.Parse(Console.ReadLine());
        long number2 = long.Parse(Console.ReadLine());
        long number3 = long.Parse(Console.ReadLine());
        long number4 = long.Parse(Console.ReadLine());
        int rows = int.Parse(Console.ReadLine());
        int columns = int.Parse(Console.ReadLine());
        long nextNumber = 0;
        long[] array = new long[rows * columns+1];
        int index = 0;

        //expressions
        array[0] = number1;
        array[1] = number2;
        array[2] = number3;
        array[3] = number4;
        for (int i = 4; i <= rows * columns; i++)
        {
            nextNumber = number1 + number2 + number3 + number4;
            array[i] = nextNumber;
            number1 = number2;
            number2 = number3;
            number3 = number4;
            number4 = nextNumber;

        }
        for (int i = 1; i <= rows; i++)
        {
            for (int n = 0; n < columns; n++)
            {
                if (n == columns - 1)
                {
                    Console.Write(array[index]);
                }
                else
                {
                    Console.Write(array[index] + " ");
                }
                index++;
            }
            Console.WriteLine();
        }
    }
}

Problem 3 – Poker

Mitko is a famous poker player. But he is not a good developer. One day he found interesting 5-card poker game which is played by a deck of 52 cards. Nothing strange here, except that the cards have no suit (color). Mitko decided to start playing this game, but he needs a program to show him the best hand that he has. Write а program to help Mitko. The program should read the cards and check each of the following conditions and display one of the messages:
• If the five cards are equal, the program outputs “Impossible”, otherwise:
• if four of them are equal, the program
outputs “Four of a Kind”, otherwise:
• if there are three equal and the another two are also equal (example: ‘2’, ‘2’, ‘10’, ‘10’, ‘2’), the program outputs “Full House”, otherwise:
• if the five are consecutive (example: ‘2’, ‘3’, ‘4’, ‘5’, ‘6’), the program outputs “Straight”, otherwise:
• if three of them are equal, the program outputs “Three of a Kind”, otherwise:
• if two pairs contain respectively equal numbers (example ‘A’, ‘K’, ‘A’, ‘K’, ‘J’), the program outputs “Two Pairs”, otherwise:
• if only two cards are equal, the program outputs “One Pair”, otherwise;
• the program outputs “Nothing”.
Cards are given with one of the following strings ‘2’, ‘3’, ‘4’, ‘5’, ‘6’, ‘7’, ‘8’, ‘9’, ‘10’, ‘J’, ‘Q’, ‘K’ or ‘A’ (without the quotes). The combination of ‘10’, ‘J’, ‘Q’, ‘K’ and ‘A’ are considered as a consecutive cards. Note that ‘A’, ‘2’, ‘3’, ‘4’ and ‘5’ are also consecutive cards so your program should output “Straight” in this case.

Input

The input data should be read from the console.
You will be given exactly 5 lines with one card per line.
The input data will always be valid and in the format described. There is no need to check it explicitly.

Output

The output data should be printed on the console.
On the only output line, write an appropriate message with the exact letters case.

Constraints

- Allowed working time for your program: 0.1 seconds. Allowed memory: 16 MB.

Examples

Example
Input
Example
output
2
7
A
J
K
Nothing
Example
input
Example
output
4
2
A
3
5
Straight

Example
input
Example
output
8
J
J
8
8
Full House



 

 

 

 

 Solution:
using System;
using System.Collections.Generic;

class Problem03
{
    static void Main()
    {
        //variables
        List<int> hand = new List<int>();
        int equalCards = 1;
        int pair = 0;
        int threeOfKind = 0;
        int fourOfKind = 0;
        int consecutive = 1;
        int currentItem;
        for (int i = 0; i < 5; i++)
        {
            string card = Console.ReadLine();
            hand.Add(Cards(card));
        }

        //expressions
        //sort the cards
        hand.Sort();
        currentItem = hand[0];

        for (int i = 1; i <= hand.Count; i++)
        {
            if (i > hand.Count - 1)
            {
                if (equalCards == 2)
                {
                    pair++;
                }
                if (equalCards == 3)
                {
                    threeOfKind++;
                }
                if (equalCards == 4)
                {
                    fourOfKind++;
                }
                break;
            }
            if (hand[i] == currentItem)
            {
                equalCards++;
            }

            else
            {
                if (hand[i] == currentItem + 1)
                {
                    consecutive++;
                }
                if (equalCards == 2)
                {
                    pair++;
                    equalCards = 1;
                }
                if (equalCards == 3)
                {
                    threeOfKind++;
                    equalCards = 1;
                }
                if (equalCards == 4)
                {
                    fourOfKind++;
                    equalCards = 1;
                }

                currentItem = hand[i];
            }
        }


        if (consecutive == 5)
        {
            Console.WriteLine("Straight");
        }
        if (consecutive == 4 && currentItem == 13)
        {
            Console.WriteLine("Straight");
        }
        if (equalCards == 5)
        {
            Console.WriteLine("Impossible");
        }
        if (fourOfKind == 1)
        {
            Console.WriteLine("Four of a Kind");
        }

        if (threeOfKind == 1 && pair == 1)
        {
            Console.WriteLine("Full House");
        }
        if (threeOfKind == 1 && pair == 0)
        {
            Console.WriteLine("Three of a Kind");
        }
        if (pair == 2)
        {
            Console.WriteLine("Two Pairs");
        }
        if (pair == 1 && threeOfKind == 0)
        {
            Console.WriteLine("One Pair");
        }
        if (pair == 0 && threeOfKind == 0 && fourOfKind == 0 && consecutive < 4 && equalCards != 5)
        {
            Console.WriteLine("Nothing");
        }

        Console.WriteLine();
    }
    //put the card in List as int
    static int Cards(string card)
    {
        int result = 0;
        switch (card)
        {
            case "A":
                result = 1;
                break;
            case "2":
                result = 2;
                break;
            case "3":
                result = 3;
                break;
            case "4":
                result = 4;
                break;
            case "5":
                result = 5;
                break;
            case "6":
                result = 6;
                break;
            case "7":
                result = 7;
                break;
            case "8":
                result = 8;
                break;
            case "9":
                result = 9;
                break;
            case "10":
                result = 10;
                break;
            case "J":
                result = 11;
                break;
            case "Q":
                result = 12;
                break;
            case "K":
                result = 13;
                break;
        }
        return result;
    }
}



Problem 4 – UK Flag

Telerik Academy is considering opening a new office in Great Britain. Therefore the whole Trainers team is traveling to the United Kingdom for the important event. They’ve decided that everyone needs to bring the UK flag with him, as a token of respect to the local citizens. Please help them and print some flags in different sizes, so they will be well received. As a little reminder, here it is the flag itself:



 Input

The input data should be read from the console.
You have an integer number N (always odd number) showing the width and the height of the flag.  The flag will always be a square.
The input data will always be valid and in the format described. There is no need to check it explicitly.

Output

The output should be printed on the console.
Use the “*” (asterisk) character for the middle, the “\”, “/”, “|” (vertical dash), “-” (dash) characters for the lines and “.” (dot) for the rest.

Constraints

 - N will always be a positive odd number between 5 and 79 inclusive.
 - Allowed working time for your program: 0.1 seconds. Allowed memory: 16 MB.

Examples

Example
input
Example
output
5
\.|./
.\|/.
--*--
./|\.
/.|.\
Example input
Example output
9
\...|.../
.\..|../.
..\.|./..
...\|/...
----*----
.../|\...
../.|.\..
./..|..\.
/...|...\














Solution: 

using System;

class Problem04
{
    static void Main()
    {
        //variables
        int n = byte.Parse(Console.ReadLine());
        int middleDots = n / 2-1;

        //expressions
        Console.WriteLine(@"\" + new String('.', middleDots) + "|" + new String('.', middleDots) + "/");

        for (int i = 1; i < n/2; i++)
        {
            Console.WriteLine(new String('.', i) + @"\" + new String('.', middleDots-1)
                                + "|" + new String('.', middleDots-1) + "/" + new String('.', i));
            middleDots--;
        }

        Console.WriteLine(new String('-', n / 2) + "*" + new String('-', n / 2));

        middleDots = n / 2 - 1;
        for (int p = n / 2-1; p >= 0; p--)
        {
            Console.WriteLine(new String('.', p) + "/" + new String('.', middleDots - (n / 2 - 1))
                                + "|" + new String('.', middleDots - (n / 2 - 1)) + @"\" + new String('.', p));
            middleDots++;
        }
    }
}


No comments:

Post a Comment