给定体积棱长为整数的棱镜的最小表面积

本文关键字:棱镜 表面积 整数 | 更新日期: 2023-09-27 18:09:55

如果我有一些int volume,我该如何写一个返回length, widthheight(所有int)的方法,以便length*width*height>=volume2(width*length+height*length+height*width)尽可能小?

基本上,给定体积的矩形棱镜的最小表面积(以及构成它的值)。

此外,理想情况下,它将能够设置length, width和/或height,它们将在解决问题时保持固定。例如,计算widthheight,同时计算volume=52length=3

编辑:我写这个简单的蛮力来告诉我我想要的答案。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace surface_area_volume_ratio
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Volume--'nSurace Area-Ratio : L, W, H");
            Console.ReadLine();
            Console.Clear();
            for (int volume = 1; volume < 200; volume++)
            {
                double dv=volume;
                List<double> ratios = new List<double>();
                List<int> surface = new List<int>();
                List<int> ls = new List<int>();
                List<int> ws = new List<int>();
                List<int> hs = new List<int>();
                for (int l = 1; l <= volume; l++)
                {
                    double dl = l;
                    for (int w = 1; w <= volume; w++)
                {
                    double dw = w;
                    for (int h = 1; h <= volume; h++)
                    {
                        double dh = h;
                        if (l * w * h >= volume)
                        {
                            int s = (2 * (l * w + l * h + w * h));
                            surface.Add(s);
                            ratios.Add(s/dv);
                            ls.Add(l);
                            ws.Add(w);
                            hs.Add(h);
                        }
                    }   
                }
            }
            double smallest=0;
            if (ratios.Count>0)
                smallest = ratios.Min();
                Console.WriteLine(volume+"--");
            for (int i = 0; i < ratios.Count; i++)
            {
                if (smallest==ratios[i])
                    Console.WriteLine("{0}-{1} : {2}, {3}, {4}",surface[i],ratios[i],ls[i],ws[i],hs[i]);
            }
            Console.ReadLine();
            Console.Clear();
        }
    }
}
}

给定体积棱长为整数的棱镜的最小表面积

计算音量:

double volume = length * width * height;

用一个立方根反转这个过程,得到长度、宽度和高度:

var val = Math.Pow(1000, ((double)1 / 3));
double length = val,
       width = val,
       height = val;

还有一个如何硬编码其中一个值的示例。您所要做的就是将硬编码的值与音量相除,然后求平方根,得到最后两个值:

double volume = 1000; //example volume
double length = 50; //<- example hard coded length
double val = Math.Sqrt(volume / length);
double width = val,
       height = val;

我最初发布这个没有意识到整数类型是问题的一个条件。我把这段代码留在这里供参考。

这是一个蛮力解决方案:

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            int volume = 52;
            int l=0, w=0, h=0, minval=int.MaxValue;
            for (int length = 1; length <= volume; length++)
            {
                for (int width = 1; width <= volume; width++)
                {
                    for (int height = 1; height <= volume; height++)
                    {
                        if(length * width * height >= volume){
                            int area = 2*(length*width + width*height + length*height);
                            if(area < minval){
                                l=length;
                                w=width;
                                h=height;
                                minval = area;
                            }
                        }
                    }
                }
            }
            Console.WriteLine("length = " + l + ", width= " + w + ", height = " + h);
        }
    }
}

求满足条件的长度为整数的矩形棱镜。如果不需要整数长度,那么问题是微不足道的,请参考caesay的答案。

恕我直言,你应该发布你的算法和你对解决方案的尝试,然后请求纠正/帮助。

从这个开始

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            int volume = 0;
            int surfaceArea = 0;
            for (int length = 1; length < 100; length++)
            {
                for (int width = 1; width < 100; width++)
                {
                    for (int height = 1; height < 100; height++)
                    {
                    }
                }
            }
        }
    }
}
​