Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add greedy algorithm #7

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
99 changes: 99 additions & 0 deletions greedy/Greedy.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApplication1
{
class Program
{
private static int _activityCount;
private static int[] _startTimes;
private static int[] _finishTimes;

static void Main(string[] args)
{
getStarted();
}

public static string Greedy(int[] start, int[] finish)
{
string a = "1";
int i = 0;
int n = start.Length;

for (int m = 1; m < n; m++)
{
if (start[m] >= finish[i])
{
a = a + ", " + (m + 1);
i = m;
}
}
return a;
}

public static void getStarted()
{
_activityCount = 0;
_startTimes = null;
_finishTimes = null;
Console.WriteLine("Greedy Activity Selector");
activityQuestion();
Console.WriteLine("Compatible activities: " + Greedy(_startTimes, _finishTimes));
Console.WriteLine();
getStarted();
}

public static void activityQuestion()
{
Console.WriteLine("Enter number of activities:");
var userInput = Console.ReadLine();
if (userInput.ToLower() == "exit") { Environment.Exit(0); }
try
{
_activityCount = Int32.Parse(userInput);
startQuestion();
}
catch
{
Console.WriteLine("You did not enter a whole number");
activityQuestion();
}
}

public static void startQuestion()
{
Console.WriteLine("Enter start times:");
var startTimes = Console.ReadLine();
if (startTimes.ToLower() == "exit") { Environment.Exit(0); }
try
{
_startTimes = startTimes.Split(new[] { ',', ' ' }, StringSplitOptions.RemoveEmptyEntries).Select(int.Parse).ToArray();
finishQuestion();
}
catch
{
Console.WriteLine("Invalid format. Separate numbers with a space or comma.");
startQuestion();
}
}

public static void finishQuestion()
{
Console.WriteLine("Enter finish times:");
var finishTimes = Console.ReadLine();
if (finishTimes.ToLower() == "exit") { Environment.Exit(0); }
try
{
_finishTimes = finishTimes.Split(new[] { ',', ' ' }, StringSplitOptions.RemoveEmptyEntries).Select(int.Parse).ToArray();
}
catch
{
Console.WriteLine("Invalid format. Separate numbers with a space or comma.");
finishQuestion();
}
}
}
}
Binary file added greedy/Greedy.exe
Binary file not shown.