I want to write a program which will accept few cut lenghts and show the user how to cut 6m steel bar to have as little offcut as possible. I want it to be easy as Im not advanced in C#.
My code doesnt work as expected. For example in the second cut it should be 3100, 2900 but it shows otherwise.
And the last bar is not needed, dont understand why it appears.
Can you have a look pls?
const int BAR_LENGHT = 6000;
List<int> cuts = new(){ 3200,800, 3100, 2900, 1200, 690, 2700, 1200, 1100, 980, 670 };
List<string> cutsOptimized = new();
cuts.Sort();
cuts.Reverse();
int cutSum = 0;
string cutGroup = "";
while(cuts.Count > 0)
{
for(int i = 0; i < cuts.Count; i++)
{
if(cutSum + cuts[i] <= BAR_LENGHT)
{
cutSum += cuts[i];
cutGroup += cuts[i].ToString() + ",";
cuts.RemoveAt(i);
}
else
{
continue;
}
}
cutsOptimized.Add(cutGroup);
cutGroup = "";
cutSum = 0;
}
Console.WriteLine(string.Join(Environment.NewLine, cutsOptimized));
//result
//3200,2700,
//3100,1200,1100,
//2900,980,690,
//1200,670,
//800,