the program takes a very long time to run because of all the combinations and the added check to filter out duplicates.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication1
{
class Program
{
const int TOTAL = 2386;
static List<int> totals = new List<int>(new int[] { 618, 350, 308, 300, 250, 232, 200, 128 });
static List<List<int>> matches = new List<List<int>>();
static void Main(string[] args)
{
List<int> numbersUsed = new List<int>();
int total = 0;
RecursiveCount(numbersUsed, total);
Console.ReadLine();
}
static void RecursiveCount(List<int> numbersUsed, int total)
{
if (numbersUsed.Count > 0)
{
//check total
if (total == 2386)
{
numbersUsed.Sort();
Boolean found = false;
foreach(List<int> match in matches)
{
if (numbersUsed.Count == match.Count)
{
Boolean rowMatch = true;
for (int i = 0; i < numbersUsed.Count; i++)
{
if (numbersUsed[i] != match[i])
{
rowMatch = false;
break;
}
}
if (rowMatch == true)
{
found = true;
break;
}
}
}
if (!found)
{
matches.Add(numbersUsed);
string[] strNumbers = numbersUsed.Select(x => x.ToString()).ToArray();
string numbers = string.Join(",", strNumbers);
Console.WriteLine(" the total of 2386 can be made from the following numbers {0}", numbers);
}
}
}
if (total < TOTAL)
{
for (int index = 0; index < totals.Count; index++)
{
int newtotal = total + totals[index];
List<int> newnumbersUsed = new List<int>(numbersUsed);
newnumbersUsed.Add(totals[index]);
RecursiveCount(newnumbersUsed, newtotal);
}
}
}
}
}
jdweng
No comments:
Post a Comment