Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- class IsoscelesTriangle
- {
- static void Main()
- {
- Console.WriteLine("Hello. You are starting a program which will try to make a triangle with given number of symbols. If you are interested, please continue");
- Console.WriteLine("Please enter a number which will represent how many symbols to use to form a triangle");
- Console.WriteLine("P.S. Does not work for numbers 3, 5, 8, 11");
- //Defining variables
- int p = Convert.ToInt32(Console.ReadLine()); //This is the number of symbols we will use
- int maxRows = 0; //The maximum of rows that we will use
- int sum = 0;
- //Making a quick check how many rows to use for the triangle
- do
- {
- if ((sum >= p) && (maxRows < p-1 / 2))
- {
- break;
- }
- maxRows++;
- sum += maxRows;
- }
- while ((sum <= p) && (maxRows < p / 2));
- int maxCols = maxRows*2 - 1; //The maximum of cols that we will use
- int[,] charsCoord = new int[maxRows, maxCols]; //Here we make the two-dimensional array which will hold the values and their positions
- bool nextSymbolIsSpace = false; //This will be used to know were we will need a space and where a symbol
- int forFillIn = p - (maxRows + 1 + (maxRows - 2) * 2); //This is the number of symbols we will put into the triangle
- int remainForFillIn = forFillIn; //This is the remaining symbols to fill-in inside the triangle
- int rem = p; //These are the remaining symbols we can use
- int spaceBeforeAfter = 0; //Here we will hold the number of spaces before and after each side on one row
- int remainSpaceBefore = 0; //Here are the remaining spaces before the triangle on one row
- int remainSpaceAfter = 0; //Here are the remaining spaces after the triangle on one row
- //Adding values to the array
- for (int r = maxRows; r >= 1; r--)
- {
- for (int c = 1; c <= maxCols; c++)
- {
- //Checking if there are remaining symbols to use
- if (rem != 0)
- {
- //Condition for the rest of the symbols which wont be on the last row
- if (r != maxRows)
- {
- //First we have to make the spaces before each row
- if (remainSpaceBefore != 0)
- {
- charsCoord[r - 1, c - 1] = 32;
- remainSpaceBefore--;
- }
- //Here we check if we can already make spaces after the triangle on each row
- else if (remainSpaceAfter != 0 && c > maxCols - spaceBeforeAfter)
- {
- if (c == maxCols)
- {
- charsCoord[r - 1, c - 1] = 32;
- spaceBeforeAfter++;
- remainSpaceBefore = spaceBeforeAfter;
- remainSpaceAfter = spaceBeforeAfter;
- }
- else
- {
- charsCoord[r - 1, c - 1] = 32;
- remainSpaceAfter--;
- }
- }
- //Now we make the symbols on this row
- else if (remainSpaceBefore == 0 && remainSpaceAfter != 0)
- {
- //If the remaining symbols for inside the triangle are zero, just add symbol at the start and at the end of the triangle
- if (remainForFillIn == 0 || (remainForFillIn != 0 && (c == maxCols - spaceBeforeAfter || c == spaceBeforeAfter + 1)))
- {
- if ((c == maxCols - spaceBeforeAfter || c == spaceBeforeAfter + 1) && nextSymbolIsSpace == true)
- {
- charsCoord[r - 1, c - 1] = 0169;
- rem--;
- nextSymbolIsSpace = true;
- }
- else if ((c == maxCols - spaceBeforeAfter || c == spaceBeforeAfter + 1) && nextSymbolIsSpace == false)
- {
- charsCoord[r - 1, c - 1] = 0169;
- rem--;
- nextSymbolIsSpace = true;
- }
- else
- {
- charsCoord[r - 1, c - 1] = 32;
- }
- }
- //But if there are still remaining symbols, we will try to place them in the triangle
- else
- {
- //If the remaining symbols can be placed on the next row, they will be placed as followed
- if (r <= 2 + forFillIn)
- {
- if (c == maxCols - spaceBeforeAfter)
- {
- charsCoord[r - 1, c - 1] = 0169;
- rem--;
- nextSymbolIsSpace = false;
- remainForFillIn--;
- }
- else if (nextSymbolIsSpace == false)
- {
- if (remainForFillIn == 1)
- {
- charsCoord[r - 1, c - 1] = 0169;
- rem--;
- nextSymbolIsSpace = false;
- remainForFillIn--;
- }
- else
- {
- charsCoord[r - 1, c - 1] = 0169;
- rem--;
- nextSymbolIsSpace = true;
- remainForFillIn--;
- }
- }
- else if (nextSymbolIsSpace == true)
- {
- charsCoord[r - 1, c - 1] = 32;
- nextSymbolIsSpace = false;
- }
- }
- //But if there are too many to be placed on one row, we will place only one part of them
- }
- }
- }
- //The array will be added with values from the last row to the first one
- //Because of that we make a special condition only for the last row
- else if (r == maxRows)
- {
- if (c == maxCols)
- {
- charsCoord[r - 1, c - 1] = 0169;
- rem--;
- nextSymbolIsSpace = false;
- spaceBeforeAfter++;
- remainSpaceBefore++;
- remainSpaceAfter++;
- }
- else
- {
- if (nextSymbolIsSpace == false)
- {
- charsCoord[r - 1, c - 1] = 0169;
- rem--;
- nextSymbolIsSpace = true;
- }
- else
- {
- charsCoord[r - 1, c - 1] = 32;
- nextSymbolIsSpace = false;
- }
- }
- }
- }
- //If there are left any remaining symbol it will execute only spaces
- else
- {
- charsCoord[r - 1, c - 1] = 32;
- }
- }
- }
- //Executing the result
- for (int r = 1; r <= maxRows; r++)
- {
- for (int c = 1; c <= maxCols; c++)
- {
- if (c == maxCols)
- {
- Console.WriteLine("{0:X}", (char)charsCoord[r - 1, c - 1]);
- }
- else
- {
- Console.Write("{0:X}", (char)charsCoord[r - 1, c - 1]);
- }
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment