我是一个相对新手的程序员。我需要根据给定的尺寸(比如axb)计算出宽高比(16:9或4:3)。请问如何在C#中实现这个功能?非常感谢您的帮助。
public string AspectRatio(int x, int y)
{
//code am looking for
return ratio
}
谢谢。
我是一个相对新手的程序员。我需要根据给定的尺寸(比如axb)计算出宽高比(16:9或4:3)。请问如何在C#中实现这个功能?非常感谢您的帮助。
public string AspectRatio(int x, int y)
{
//code am looking for
return ratio
}
谢谢。
你需要找到最大公约数,然后将x和y都除以它。
static int GCD(int a, int b)
{
int Remainder;
while( b != 0 )
{
Remainder = a % b;
a = b;
b = Remainder;
}
return a;
}
return string.Format("{0}:{1}",x/GCD(x,y), y/GCD(x,y));
提示
如果你想处理像 16:10 这样的比例(可以被两个数字整除),你需要准备一个预定义表,其中包含((float)x)/y
- 比例对。
既然您只需要在16:9和4:3之间做出选择,这里有一个更简单的解决方案。
public string AspectRatio(int x, int y)
{
double value = (double)x / y;
if (value > 1.7)
return "16:9";
else
return "4:3";
}
return x/GCD + ":" + y/GCD;
16:10
的情况失败了... ;) - Lucero