Initial data commit

This commit is contained in:
Ingy döt Net 2023-07-01 11:58:00 -04:00
commit f23f22d71c
199087 changed files with 3378941 additions and 0 deletions

View file

@ -0,0 +1,42 @@
namespace ConsoleApplication1
{
using System;
class Program
{
static void Main(string[] args)
{
bool[] doors = new bool[100];
//Close all doors to start.
for (int d = 0; d < 100; d++) doors[d] = false;
//For each pass...
for (int p = 0; p < 100; p++)//number of passes
{
//For each door to toggle...
for (int d = 0; d < 100; d++)//door number
{
if ((d + 1) % (p + 1) == 0)
{
doors[d] = !doors[d];
}
}
}
//Output the results.
Console.WriteLine("Passes Completed!!! Here are the results: \r\n");
for (int d = 0; d < 100; d++)
{
if (doors[d])
{
Console.WriteLine(String.Format("Door #{0}: Open", d + 1));
}
else
{
Console.WriteLine(String.Format("Door #{0}: Closed", d + 1));
}
}
Console.ReadKey(true);
}
}
}

View file

@ -0,0 +1,21 @@
namespace ConsoleApplication1
{
using System;
class Program
{
static void Main(string[] args)
{
//Perform the operation.
bool[] doors = new bool[100];
int n = 0;
int d;
while ((d = (++n * n)) <= 100)
doors[d - 1] = true;
//Perform the presentation.
for (d = 0; d < doors.Length; d++)
Console.WriteLine("Door #{0}: {1}", d + 1, doors[d] ? "Open" : "Closed");
Console.ReadKey(true);
}
}
}

View file

@ -0,0 +1,19 @@
namespace ConsoleApplication1
{
using System;
class Program
{
static void Main()
{
bool[] doors = new bool[100];
//The number of passes can be 1-based, but the number of doors must be 0-based.
for (int p = 1; p <= 100; p++)
for (int d = p - 1; d < 100; d += p)
doors[d] = !doors[d];
for (int d = 0; d < 100; d++)
Console.WriteLine("Door #{0}: {1}", d + 1, doors[d] ? "Open" : "Closed");
Console.ReadKey(true);
}
}
}

View file

@ -0,0 +1,16 @@
namespace ConsoleApplication1
{
using System;
class Program
{
static void Main()
{
double n;
//If the current door number is the perfect square of an integer, say it is open, else say it is closed.
for (int d = 1; d <= 100; d++)
Console.WriteLine("Door #{0}: {1}", d, (n = Math.Sqrt(d)) == (int)n ? "Open" : "Closed");
Console.ReadKey(true);
}
}
}

View file

@ -0,0 +1,96 @@
using System;
using System.IO;
using System.Collections.Generic;
class Program
{
static void Main()
{
Console.Clear();
Console.WriteLine("Input a number of doors to calculate, then press enter");
StartCalculator();
}
static void StartCalculator()
{
//The number to calculate is input here
string input = Console.ReadLine();
Console.Clear();
try
{
//The program attempts to convert the string to an int
//Exceptions will be caught on this line
int numberOfDoors = Convert.ToInt32(input);
//Will call method recursively if input number is less than 1
if (numberOfDoors <= 0)
{
Console.WriteLine("Please use a number greater than 0");
StartCalculator();
}
//The program then starts the calculation process
Calculate(numberOfDoors);
//After calculation process is finished, restart method is called
RestartCalculator();
}
catch(FormatException)
{
//Code will be executed if the number has a decimal or has an unrecognizable symbol
Console.WriteLine("Unable to read. Please use a real number without a decimal");
StartCalculator();
}
catch (OverflowException)
{
//Code will be executed if number is too long
Console.WriteLine("You number is too long");
StartCalculator();
}
}
static void Calculate(int numberOfDoors)
{
//Increases numberOfDoors by 1 since array starts at 0
numberOfDoors++;
//Dictionary key represents door number, value represents if the door is open
//if value == true, the door is open
Dictionary<int, bool> doors = new Dictionary<int, bool>();
//Creates Dictionary size of numberOfDoors, all initialized at false
for(int i = 0; i < numberOfDoors; i++)
{
doors.Add(i, false);
}
//Creates interval between doors, starting at 0, while less than numberOfDoors
for (int doorInterval = 0; doorInterval < numberOfDoors; doorInterval++)
{
//Will alter every cubby at doorInterval
//1 needs to be added since doorInterval will start at 0 and end when equal to numberOfDoors
for(int i = 0; i < numberOfDoors; i += doorInterval + 1)
{
//Changes a false value to true and vice versa
doors[i] = doors[i] ? false: true;
}
}
//Writes each door and whether it is open or closed
for(int i = 0; i < numberOfDoors; i++)
{
//Skips over door 0
if (i == 0) continue;
//Writes open if door value is true, writes closed if door value is false
Console.WriteLine("Door " + (i) + " is " + (doors[i] ? "open" : "closed"));
}
}
static void RestartCalculator()
{
Console.WriteLine("Press any key to restart");
Console.ReadKey(true);
Main();
}
}