Задача 9
Write a program to print the first 100 members of the sequence of Fibonacci: 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, …
using System; using System.Numerics; class Fibonacci { static void Main() { Console.Write("Enter how much numbers from the Fibonacci sequence you want calculated:"); byte n; if ((!byte.TryParse(Console.ReadLine(), out n)) || (n < 1)) { Console.Write("Incorrect number, we will diplay the first 100 numbers."); n = 100; } BigInteger currentNumber = 0; BigInteger lastNumber = 1; for (int i = 1; i <= n; i++) { Console.WriteLine("Number {0,-3}:{1}", i, currentNumber); currentNumber = currentNumber + lastNumber; lastNumber = currentNumber - lastNumber; } } }
Задача 10
Write a program to calculate the sum (with accuracy of 0.001): 1 + 1/2 - 1/3 + 1/4 - 1/5 + ...
using System; class CalculateSumFractions { static void Main() { ushort n = 1000; decimal sum = 1; for (int i = 2; i <= n; i++) { if ((i & 1) == 1) { sum -= 1m / i; } else { sum += 1m / i; } } Console.WriteLine("The sum is: {0:F3}", sum); //Second solution sum = 1; ushort devisor = 2; while (1m / devisor >= 0.001m) { if ((devisor & 1) == 1) { sum -= 1m / devisor; } else { sum += 1m / devisor; } devisor++; } Console.WriteLine("The sum from the second solution is: {0:F3}", sum); } }
Задача 11
Implement the "Falling Rocks" game in the text console. A small dwarf stays at the bottom of the screen and can move left and right (by the arrows keys). A number of rocks of different sizes and forms constantly fall down and you need to avoid a crash.
using System; using System.Collections.Generic; using System.Collections; using System.Text; using System.Threading; class FallingRocks { struct Position { public int row; public int col; public Position(int row, int col) { this.row = row; this.col = col; } public void ChangePosition (int row, int col) { this.row += row; this.col += col; } } struct Rock { public char symbol; public int weight; public Position position; public Rock(char symbol, int weight, int row = 0, int col = 0) { this.symbol = symbol; this.weight = weight; this.position.row = row; this.position.col = col; } } static void Main(string[] args) { byte maxNumberOfRocksPerRow = 3; Random randRocksPerRow = new Random(); Queue<Rock> rocksQueue = new Queue<Rock> { }; Position[] positions = new Position[] { new Position(0,1), new Position(0,-1) }; Rock[] rocksArr = new Rock[] { new Rock ('^', 10), new Rock ('@', 20), new Rock ('*', 30), new Rock ('&', 40), new Rock ('+', 50), new Rock ('%', 60), new Rock ('$', 70), new Rock ('#', 80), new Rock ('!', 90), new Rock ('.', 100), new Rock (';', 110), new Rock ('-', 120) }; int rockTotalWeight = 0; for (int i = 0; i < rocksArr.Length; i++) { rockTotalWeight = rocksArr[i].weight; rocksArr[i].weight = rockTotalWeight; } Random randRocksType = new Random(); Position dworf = new Position(Console.WindowHeight-1, Console.WindowWidth/2 - 2); Console.BufferHeight = Console.WindowHeight; Random randRocksColPosition = new Random(); int rowNumber = 0; bool gameOn = true; while (gameOn) { if (Console.KeyAvailable) { ConsoleKeyInfo userInput = Console.ReadKey(); if ((userInput.Key == ConsoleKey.RightArrow) && (dworf.col < Console.WindowWidth-4)) { dworf.ChangePosition(0, 1); } else if ((userInput.Key == ConsoleKey.LeftArrow) && (dworf.col > 0)) { dworf.ChangePosition(0, -1); } }; int randomRockWeight; int randomRockNumberPerRow = randRocksPerRow.Next(maxNumberOfRocksPerRow); Rock tempRock = new Rock(); for (int i = 0; i < randomRockNumberPerRow; i++) { randomRockWeight = randRocksType.Next(rockTotalWeight); int k = 0; while (rocksArr[k].weight < randomRockWeight) { k++; } rocksQueue.Enqueue(new Rock(rocksArr[k].symbol, rocksArr[k].weight, rowNumber, randRocksColPosition.Next(Console.WindowWidth))); } Console.Clear(); //We remove the rocks that are already under the last visible row try { tempRock = rocksQueue.Peek(); while ((rowNumber - tempRock.position.row) > (Console.WindowHeight - 1)) { rocksQueue.Dequeue(); tempRock = rocksQueue.Peek(); } } catch { } int currentRockRow = 0; //We visualize the rocks foreach (Rock rockInQueue in rocksQueue) { currentRockRow = rowNumber - rockInQueue.position.row; if ((currentRockRow == (Console.WindowHeight-1)) && (rockInQueue.position.col >= dworf.col) && (rockInQueue.position.col <= (dworf.col+2))) { gameOn = false; break; } else { Console.SetCursorPosition(rockInQueue.position.col, rowNumber - rockInQueue.position.row); Console.Write(rockInQueue.symbol); } } Console.SetCursorPosition(dworf.col, dworf.row); Console.Write("(0)"); Thread.Sleep(150); rowNumber++; } Console.Clear(); //Console.SetCursorPosition((Console.WindowHeight / 2), (Console.WindowWidth / 2 - 5)); Console.SetCursorPosition((Console.WindowWidth / 2 - 5), (Console.WindowHeight / 2)); Console.WriteLine("************"); Console.SetCursorPosition((Console.WindowWidth / 2 - 5), (Console.WindowHeight / 2) + 1); Console.WriteLine("*Game over!*"); Console.SetCursorPosition((Console.WindowWidth / 2 - 5), (Console.WindowHeight / 2) + 2); Console.WriteLine("************"); } }
Няма коментари:
Публикуване на коментар