C# 具有整数长度边的给定体积棱镜的最小表面积
如果我有一些C# 具有整数长度边的给定体积棱镜的最小表面积,c#,geometry,formula,C#,Geometry,Formula,如果我有一些int-volume,我将如何编写一个返回length、width和height(所有int)的方法,以便length*width*height>=volume和2(width*length+height*width)尽可能小 基本上,给定体积的矩形棱镜的最小表面积(及其组成值) 此外,理想情况下,它将能够设置长度、宽度、和/或高度,并且在解决问题时它们将保持不变。例如,计算宽度和高度,而体积=52和长度=3 编辑:我写了这个简单的暴力来告诉我我想要的答案 using System;
int-volume
,我将如何编写一个返回length
、width
和height
(所有int)的方法,以便length*width*height>=volume
和2(width*length+height*width)
尽可能小
基本上,给定体积的矩形棱镜的最小表面积(及其组成值)
此外,理想情况下,它将能够设置长度
、宽度
、和/或高度
,并且在解决问题时它们将保持不变。例如,计算宽度
和高度
,而体积=52
和长度=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();
}
}
}
}
使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用系统文本;
使用System.Threading.Tasks;
名称空间表面积体积比
{
班级计划
{
静态void Main(字符串[]参数)
{
Console.WriteLine(“体积--\n截面积比:长、宽、高”);
Console.ReadLine();
Console.Clear();
用于(int volume=1;volume<200;volume++)
{
双dv=体积;
列表比率=新列表();
列表表面=新列表();
列表ls=新列表();
List ws=新列表();
List hs=新列表();
对于(int l=1;l从这个开始
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++)
{
}
}
}
}
}
}
使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用系统文本;
命名空间控制台应用程序1
{
班级计划
{
静态void Main(字符串[]参数)
{
int体积=0;
int surfaceearea=0;
用于(int-length=1;长度<100;长度++)
{
用于(整数宽度=1;宽度<100;宽度++)
{
对于(int height=1;height<100;height++)
{
}
}
}
}
}
}
按如下方式计算体积:
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;
double volume=1000;//示例卷
double length=50;//这里有一个暴力解决方案:
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);
}
}
}
命名空间控制台应用程序1
{
班级计划
{
静态void Main(字符串[]参数)
{
int体积=52;
int l=0,w=0,h=0,minval=int.MaxValue;
对于(int-length=1;length),我不明白为什么这会有帮助。@caesay因为这是最基本方法的一个很好的起点,并且可能比实际算法(对于小数字)更容易.我们讨论的是哪种形状?添加您针对问题提出的解决方案并获得实施帮助。这样,您将更好地了解如何将您的想法转换为c#“不知道从何处开始”=>还没有准备好询问堆栈溢出问题。如果你不知道如何在纸上进行,你需要先弄清楚这一点,如果你需要帮助,可以在数学Q+a网站上询问。有指向你正在寻找的问题类型的链接…这可以通过介绍性微积分轻松解决。这是实际问题的解决方法吗“…所有INT…表面尽可能小"?@AlexeiLevenkov:如何尽可能小?更具体一点。如果你有相同的卷,它将始终是相同的大小。相同的卷不会生成更小或更大的多维数据集。这将生成一个具有相等xyz值的多维数据集,因此它是所有三个卷的最小可能值。减少任何一个值都只会增加一个值Other.caesay-问题要求所有值都是int。这使得问题变得更加困难-例如,您的代码如何找到一组int值作为65的答案?请注意,出于某种原因,类似的建议已经被否决了…无论如何,对于相对较小的数目的暴力是最简单的方法。该建议只是有代码框架,而不是c如果您发现答案有任何错误,请解释,我很乐意进行编辑。