Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/273.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/algorithm/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 将长度为n的杆最佳切割为3个或4个尺寸中的任意一个_C#_Algorithm - Fatal编程技术网

C# 将长度为n的杆最佳切割为3个或4个尺寸中的任意一个

C# 将长度为n的杆最佳切割为3个或4个尺寸中的任意一个,c#,algorithm,C#,Algorithm,我不确定我是否在堆栈上的正确区域,所以我首先道歉 我需要知道如何计算3种不同固定长度的所有可能组合,这些组合可以适合n长度的杆 例如,如果我有3个固定长度的8,10,12和一个可变长度的杆,比如说50';我想知道我能做的所有可能的切割。使用: 在这里,我们试图最小化杆长度和限制项目数量的项目总和之间的差异,在您的案例中,最大50/8=6个项目。以下是代码示例: using System; using System.Collections.Generic; using System.Linq; u

我不确定我是否在堆栈上的正确区域,所以我首先道歉

我需要知道如何计算3种不同固定长度的所有可能组合,这些组合可以适合n长度的杆

例如,如果我有3个固定长度的8,10,12和一个可变长度的杆,比如说50';我想知道我能做的所有可能的切割。

使用:


在这里,我们试图最小化杆长度和限制项目数量的项目总和之间的差异,在您的案例中,最大50/8=6个项目。

以下是代码示例:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace ConsoleApplication104
{
    class Program
    {
        static void Main(string[] args)
        {
            string max = "111111";
            int size = 50;
            List<List<int>> numbers = CountModThree(max,size);
            Print(numbers);
            Console.ReadLine();


        }
        static List<List<int>> CountModThree(string max,int size)
        {
            List<List<int>> results = new List<List<int>>();
            List<int> newMod3 = new List<int>() {0};
            while(true)
            {
                int length = newMod3.Select(x => x == 0 ? 8 : x == 1 ? 10 : 12).Sum();
                if (length <= size) results.Add(newMod3);
                if (string.Join("", newMod3) == max) break;

                newMod3 = AddOne(newMod3);
            }
            return results;
        }
        static List<int> AddOne(List<int> number)
        {
            List<int> newNumber = new List<int>();
            newNumber.AddRange(number);

            int carry = 1;
            for (int i = number.Count - 1; i >= 0; i--)
            {
                int digit = newNumber[i] + carry;
                if (digit == 3)
                {
                    newNumber[i] = 0;
                    carry = 1;
                }
                else
                {
                    newNumber[i] = digit;
                    carry = 0;
                    break;
                }
            }
            if (carry == 1) newNumber.Insert(0, 0);
            return newNumber;
        }
        static void Print(List<List<int>> numbers)
        {
            foreach(List<int> number in numbers)
            {
                Console.WriteLine("string : '{0}', Total Length : '{1}, Number 8ft sections : '{2}', Number 10ft sections : '{3}', Number 12ft sections : '{4}'",
                    string.Join("", number),
                    number.Select(x => x == 0 ? 8 : x == 1 ? 10 : 12).Sum(),
                    number.Where(x => x == 0).Count(),
                    number.Where(x => x == 1).Count(),
                    number.Where(x => x == 2).Count()
                    );
            }

        }
    }
}

您希望将n的所有分区划分为预定义的整数部分。简单的方法是使用递归

Python代码提供了所有可能的解决方案,由于上次使用了索引,所以忽略了summands顺序。因此,8 8 10和8 8 10 8 8 8 8 8 8变种被认为是相同的。若需要区分它们,只需删除最后一个参数并从0开始循环

def solution(sum, lst, last, sol):
    if sum == 0:
        print(sol)
        return
    for i in range(last, len(lst)):
        if lst[i] <= sum:
            solution(sum - lst[i], lst, i, sol + " " + str(lst[i]))
    return

solution(50, [8,10,12], 0, "")

 8 8 8 8 8 10
 8 8 10 12 12
 8 10 10 10 12
 10 10 10 10 10

你想知道a+b+c这些值8-10-12和50的预期输出是什么?最佳值是指a*8+b*10+c*12尽可能接近50?例如,a=0、b=5和c=0是最好的解决方案之一。如果范围不太广,则可以接受简单的解决方案。否则,这通常是解决者会遇到的问题。。。例如,解决微软解决方案基金会。我知道。你只需数到最大值并进行测试,以确保没有一个超过50英尺。例如,5个2的22222小于6个零,但5*12=60英尺,你可以跳过。我正在查看它。。并将其放入我的测试应用程序中
def solution(sum, lst, last, sol):
    if sum == 0:
        print(sol)
        return
    for i in range(last, len(lst)):
        if lst[i] <= sum:
            solution(sum - lst[i], lst, i, sol + " " + str(lst[i]))
    return

solution(50, [8,10,12], 0, "")

 8 8 8 8 8 10
 8 8 10 12 12
 8 10 10 10 12
 10 10 10 10 10
dict = [[]  for i in range(10)]

def solution(sum, lst, last, sol):
    if sum == 0:
        print(sol)
        return
    if sum < 10:
        dict[sum].append(sol)
    for i in range(last, len(lst)):
        if lst[i] <= sum:
            solution(sum - lst[i], lst, i, sol + " " + str(lst[i]))
    return

solution(50, [5, 13 , 21], 0, "")
for i in range(1, len(dict)):
    if len(dict[i]) > 0:
        print(dict[i])
        break

  5 5 5 5 5 5 5 5 5 5  #exact
  [' 5 5 5 13 21', ' 5 5 13 13 13']  #inexact but close
def tablecuts(sum, lst):
    cuts = [[]  for i in range(sum+1)]
    cuts[0].append('*')
    for item in lst:
        for i in range(0, sum - item + 1):
            if len(cuts[i]) > 0:
                for j in cuts[i]:
                    cuts[i+item].append(j+" " + str(item))
    for i in range(len(cuts)):
         print(i, cuts[i])

tablecuts(13, [2,5,7])

0 ['*']
1 []
2 ['* 2']
3 []
4 ['* 2 2']
5 ['* 5']
6 ['* 2 2 2']
7 ['* 2 5', '* 7']
8 ['* 2 2 2 2']
9 ['* 2 2 5', '* 2 7']
10 ['* 2 2 2 2 2', '* 5 5']
11 ['* 2 2 2 5', '* 2 2 7']
12 ['* 2 2 2 2 2 2', '* 2 5 5', '* 5 7']
13 ['* 2 2 2 2 5', '* 2 2 2 7']