settingsAccountsettings
Menusettings

Q: Problem solving in C# Task - Dumbbell

+3 votes

As we all know programmers are not bodybuilders and vice versa. But there is one exception. His name is Marcho Zukerberov.  At the moment he is very busy learning how to code in the well-known language “C #” and he has only 30 minutes per day for training. Help Marcho break the rule of becoming an ordinary and overweight programmer by making him a dumbbell with the weight needed (N kg).

Input:

The input data should be read from the console.

On the only input line you have an integer number N, showing the height of the dumbbell.

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 number of lines should be equal to the height N of the dumbbell.

Each line should hold exactly N x 3 symbols: "." (dot) , "*" (asterisk), "=" (equal sign) or “&” (ampersand).

The visible part of the dumbbell bar should be exactly N symbols.

Constraints:

  • The number N will always be an odd integer number in the range [5…39].
  • Allowed working time for your program: 0.25 seconds.
  • Allowed memory: 16 MB.

Examples:

dumbell task in c sharp

asked in C# category by user eiorgert
edited by user golearnweb

1 Answer

+1 vote
 
Best answer

First you need to devide the task into sub-task (smaller ones) and then decide which rows will be static and which will be dynamic. The dynamic ones can be solved with the for loop + using the i (the changeable iterator i); and the static ones can be printed as they are :-)

static and dynamic rows in c sharp - solving the dumbell task

Here is my screenshot - the yellow rows are the static ones - the easier ones and the others are the dynamic ones (harder to solve); Simply think about the interconnections between the n, i and some values and the task won't seem so hard... It took me around 1 hour to find the solution...

Here it is: 

using System;
class Dumbbell
{
    static void Main()
    {
        int n = int.Parse(Console.ReadLine());

        Console.WriteLine("{0}{1}{2}{1}{0}",
                new string(('.'), n / 2),
                new string(('&'), n / 2 + 1),
                new string(('.'), n));

        for (int i = 0; i < n / 2 - 1; i++)
        {
            Console.WriteLine("{0}&{1}&{2}&{1}&{0}",
                    new string(('.'), n / 2 - 1 - i),
                    new string(('*'), n / 2 + i),
                    new string(('.'), n));
        }
        Console.WriteLine("&{0}&{1}&{0}&",
                    new string(('*'), n - 2),
                    new string(('='), n));

        for (int i = 0; i < n / 2 - 1; i++)
        {
            Console.WriteLine("{0}&{1}&{2}&{1}&{0}",
                    new string(('.'), i+1),    
                    new string(('*'), n-3-i),
                    new string(('.'), n));
        }

        Console.WriteLine("{0}{1}{2}{1}{0}",
            new string (('.'),n/2),
            new string (('&'),n/2+1),
            new string (('.'),n));
    }
}
answered by user nikole
edited by user golearnweb
...